如何迭代对象数组并仅获取特定值

时间:2018-02-16 13:25:22

标签: javascript json node.js

我有一个格式低于

的数组
[{
    "_id" : "500",
    "loanRef" : "500",
    "createdTime" : "2018-02-15T17:20:47.156Z",
    "bdetails" : {
        "config" : {
            "chase" : [
                6,
                12
            ],
            "expiry" : 35
        }
    },
},
{
    "_id" : "500",
    "loanRef" : "500",
    "createdTime" : "2018-02-15T18:11:45.377Z",
    "bdetails" : {
        "config" : {
            "chase" : [
                6,
                12
            ],
            "expiry" : 35
        }
    },
}
}]

以上数组大小约为200 ..

但我想用这种格式

[{
    "_id" : "500",
    "loanRef" : "500",
    "createdTime" : "2018-02-15T17:20:47.156Z",
    "chase" : "[6,12]",
    "expiry" : 35           
},
{
    "_id" : "500",
    "loanRef" : "500",
    "createdTime" : "2018-02-15T18:11:45.377Z",
    "chase" : "[6,12]",
    "expiry" : 35
}]

有人可以通过一些js逻辑来帮助我们如何形成这个。任何帮助都可以拯救我。谢谢

8 个答案:

答案 0 :(得分:6)

您可以将功能forEachJSON.stringifySpread operator (...)一起使用。

var array = [{     "_id" : "500",     "loanRef" : "500",     "createdTime" : "2018-02-15T17:20:47.156Z",     "bdetails" : {         "config" : {             "chase" : [                 6,                 12             ],             "expiry" : 35         }     }, }, {     "_id" : "500",     "loanRef" : "500",     "createdTime" : "2018-02-15T18:11:45.377Z",     "bdetails" : {         "config" : {             "chase" : [                 6,                 12             ],             "expiry" : 35         }     }, } ];

array.forEach((o) => {
  Object.assign(o, o.bdetails.config );
  o.chase = JSON.stringify(o.chase);
  delete o.bdetails;
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:3)

您可以使用Array.map将每个项目转换为其他对象;

const data = [{
    "_id": "500",
    "loanRef": "500",
    "createdTime": "2018-02-15T17:20:47.156Z",
    "bdetails": {
      "config": {
        "chase": [
          6,
          12
        ],
        "expiry": 35
      }
    },
  },
  {
    "_id": "500",
    "loanRef": "500",
    "createdTime": "2018-02-15T18:11:45.377Z",
    "bdetails": {
      "config": {
        "chase": [
          6,
          12
        ],
        "expiry": 35
      }
    },
  }
];

const result = data.map(item => ({
  "_id": item.id,
  "loanRef": item.loadRef,
  "createdTime": item.createdTime,
  "chase": JSON.stringify(item.bdetails.config.chase),
  "expiry": item.bdetails.config.expiry
}));

console.log(result);

答案 2 :(得分:2)

您可以对所需属性进行破坏并使用它返回一个新对象。



var array = [{ _id: "500", loanRef: "500", createdTime: "2018-02-15T17:20:47.156Z", bdetails: { config: { chase: [6, 12], expiry: 35 } } }, { _id: "500", loanRef: "500", createdTime: "2018-02-15T18:11:45.377Z", bdetails: { config: { chase: [6, 12], expiry: 35 } } }],
    result = array.map(
        ({ _id, loanRef, createdTime, bdetails: { config: { chase, expiry } } }) =>
            ({ _id, loanRef, createdTime, chase: JSON.stringify(chase), expiry })
    );

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 3 :(得分:1)

你应该试试

objects.map(o => ({
    _id: o._id,
    loanRef: o.loanRef,
    createdTime: o.createdTime,
    chase : o.bdetails.config.chase,
    expiry : o.bdetails.config.expiry
});

答案 4 :(得分:0)

没有原型功能:

 function map(arr) {
        for(var i = 0; i < arr.length; i++) {
            var item = arr[i];
            item['chase'] = item.bdetails.config.chase;
            item['expiry'] = item.bdetails.config.expiry;
            delete item.bdetails;
        }
    }

答案 5 :(得分:0)

为避免使用delete,您可以迭代旧数组并仅将所需数据添加到新数组中。

另外,我以一种我认为更容易适应不同情况的方式编写它。

let newArray = [];

oldArray.forEach( e => {
  let obj = {};
  Object.keys(e).forEach( k => {
    if (k != "bdetails"){
     obj[k] = e[k];
    }else{
     obj.merge(e[k]);
    }
  });
  newArray.push(obj);
)

答案 6 :(得分:0)

这应该可以解决问题

var a = [{...},{...}]     
for (var i = 0;i < a.length; i++) {
      var temp = {}
      temp['_id'] = a[i]['_id']
      ...
      a[i] = temp;
}

答案 7 :(得分:0)

使用map进行ES6解构的另一种看法:

const out = data.map(({ bdetails: {config: { chase, expiry } }, ...rest }) => {
  return { ...rest, expiry, chase: JSON.stringify(chase) };
});