使用 filter 返回对象中的属性值

尝试创建一个使用 filter 但不使用 for 或 while 循环或 foreach 函数的函数,该函数将循环通过对象数组,只返回对象的属性值。比如说,

function getShortMessages(messages) {
return messages.filter(function(obj){
return obj.message
});
}

所以如果我打电话

getShortMessages([{message:"bleh"},{message:"blah"}]);

我应该得到一个数组的返回值 = [“ bleh”,“ brah”] 我只是不确定如何在这些指导方针下实现过滤。我还想用一个链式函数。地图。

下面是完整的代码挑战规范

基础: 过滤器 18个练习中的第4个

任务

使用 Array # filter 编写一个名为 getShortMessages 的函数。

GetShortMessages 接受具有’的对象数组。属性,并返回长度小于 < 50个字符的消息数组。

函数应该返回一个包含消息本身的数组,而不包含消息的对象。

争论

  • 消息: 一个由10到100个随机对象组成的数组,看起来像这样:
{
message: 'Esse id amet quis eu esse aute officia ipsum.' // random
}

条件

  • 不要使用任何 for/while 循环或 Array # foreach。
  • 不要创建任何不必要的函数,例如:。

提示

  • 尝试链接一些 Array 方法!

例子

[ 'Tempor quis esse consequat sunt ea eiusmod.',
'Id culpa ad proident ad nulla laborum incididunt.',
'Ullamco in ea et ad anim anim ullamco est.',
'Est ut irure irure nisi.' ]

资源

例行公事

function getShortMessages(messages) {
// SOLUTION GOES HERE
}


module.exports = getShortMessages

要再次打印这些指令,运行: function-javascript print ”要在测试环境中执行程序,运行: function-javascript run program. js ”要验证您的程序,请运行: function-javascript 演示验证 program. js ”关于帮助运行: function-javascript help

149480 次浏览

Use .filter when you want to get the whole object(s) that match the expected property or properties. Use .map when you have an array of things and want to do some operation on those things and get the result.

The challenge is to get all of the messages that are 50 characters or less. So you can use filter to get only the messages that pass that test and then map to get only the message text.

function getShortMessages(messages) {
return messages
.filter(function(obj) {
return obj.message.length <= 50;
})
.map(function(obj) {
return obj.message;
});
}

JSFiddle: http://jsfiddle.net/rbbk65sq/

If it is possible for the input objects to not have a message property, you would want to test for it with obj.message && obj.message.length <= 50 like this:

function getShortMessages(messages) {
return messages
.filter(function(obj) {
return obj.message && obj.message.length <= 50;
})
.map(function(obj) {
return obj.message;
});
}

ES6

The same code samples in ES6:

const getShortMessages = (messages) => messages
.filter(obj => obj.message.length <= 50)
.map(obj => obj.message);

And if the input objects might not have the message property:

const getShortMessages = (messages) => messages
.filter(obj => obj.message && obj.message.length <= 50)
.map(obj => obj.message);

JSFiddle: http://jsfiddle.net/npfsrwjq/

Though I realize this thread is super old, I felt it necessary to comment in the event that someone stumbles on it again. I would do it like above just using es6 syntax like this:

objects.filter(obj => obj.key === 'value').map(filteredObj => filteredObj.key);

So the above example would be:

getShortMessages = (messages) => messages.filter(obj => obj.message.length <= 50).map(obj => obj.message);

With custom return value (Simple ES6 Example);

const customReturnFiltere = (result) => {
return products.filter((obj) => {
return obj.stock !== 0;
})
.map((finalResult) => {
return {
...finalResult,
info: 'product will be filtered if stock is 0'
}
});
}

In case someone is looking for a single pass through it can be done with .reduce()

So instead of doing:

const getShortMessages = (messages) => messages
.filter(obj => obj.message.length <= 50)
.map(obj => obj.message);

You can do:

const getShortMessages = (messages) => {
return messages.reduce((shortMessages, i) => {
if (i.message.length <= 50) {
shortMessages.push(i.message)
}
return shortMessages
},[])
}

Or if you want even shorter hand:

const getShortMessages = (messages) => messages.reduce((shortMessages, item) => (item.message.length <= 50)
? shortMessages=[...shortMessages,item.message] : shortMessages,[])

In addition, I am using ES6 Destructure with filter and compare two object array. And finally map specific fields those i need actually.

Initialization

const bumperDealRules =[]; // Array object
const cartItems =[]; // Array object

Final code

const bumperDealsSelected = bumperDealRules.filter(
({ item_display_id: item_display_id, purchasequantity: purchasequantity })
=> cartItems.some(({ id: id, action_from: action_from, order_qty:  order_qty })
=> id === item_display_id && purchasequantity <= order_qty &&  action_from == 'bumper_deal' )
).map(function(obj) {
return {
bumper_deal_company_name_id: obj.bumper_deal_company_name_id,
from_amount: obj.from_amount,
to_amount: obj.to_amount,
discount: obj.discount,
highest_limit: obj.highest_limit
};
});