我们说我有以下内容:
[
{ messageid: '582b4350af7cb8f21e4b7b43',
_id: 582b4c79105387dd21e08004 } ,
{ messageid: '582b4350af7cb8f21e4b7xb43',
_id: 582b4c79105387dd21e08s004 }
]
如何让它生成一个只有messageid
值的数组?
像:
{582b4350af7cb8f21e4b7b43,582b4350af7cb8f21e4b7xb43}
?
注意:普通的javascript或角度
答案 0 :(得分:2)
您可以使用http://fsharpforfunandprofit.com/posts/fvsc-quicksort/
var output = input.map(function(item) {
return item.messageid;
});
这将创建一个看起来像[582b4350af7cb8f21e4b7b43,582b4350af7cb8f21e4b7xb43]
答案 1 :(得分:2)
您可以映射属性messageid
。
var objectArray = [{ messageid: '582b4350af7cb8f21e4b7b43', _id: '582b4c79105387dd21e08004' }, { messageid: '582b4350af7cb8f21e4b7xb43', _id: '582b4c79105387dd21e08s004' }],
newArray = objectArray.map(function (o) {
return o.messageid;
});
console.log(newArray);
答案 2 :(得分:1)
使用forEach
循环遍历数组
var orArray = [{
messageid: '582b4350af7cb8f21e4b7b43',
_id: '582b4c79105387dd21e08004'
}, {
messageid: '582b4350af7cb8f21e4b7xb43',
_id: '582b4c79105387dd21e08s004'
}
];
var newArr = [];
orArray.forEach(function(item) {
newArr.push(item.messageid)
});
console.log(newArr)