JavaScript只通过messageid使对象成为数组

时间:2016-11-15 18:02:21

标签: javascript angularjs arrays object

我们说我有以下内容:

  [
 { messageid: '582b4350af7cb8f21e4b7b43',
        _id: 582b4c79105387dd21e08004 } ,
 { messageid: '582b4350af7cb8f21e4b7xb43',
        _id: 582b4c79105387dd21e08s004 } 

]

如何让它生成一个只有messageid值的数组?

像:

{582b4350af7cb8f21e4b7b43,582b4350af7cb8f21e4b7xb43}

注意:普通的javascript或角度

3 个答案:

答案 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)

DEMO