如何将数组的数组映射到诸如属性和值之类的对象? [英] How to map an Array of arrays to object like property and value?

查看:49
本文介绍了如何将数组的数组映射到诸如属性和值之类的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javaScript的新手,会做出反应.

我有一个由对象组成的数组.我需要在AG GRID中显示此数组,以便在此处将其映射到平面obj数组.

注意:我无法使用jQuery.

下面是我的数组(完整的代码在codepen中,请检查链接)

  let realData = [[{"key":"flightNumber","label":航班号",值":"EK 131","value-type":"String",订单":1},{"key":"flightId","label":"flightId","value":"1223434","value-type":"String",订单":2"isId":true},{"key":"CallSign","label":"callSign","value":"123","value-type":"String",订单":3},]] 

我想要的结果是

  let newArray = [{flightNumber:"EK 131",flightType:"A",lastUpdate:"11223232",destOrig:"BEY",sibtSobt:"12121333123",CallSign:"123"},{flightNumber:"EK 132",flightType:"D",lastUpdate:"11334455",destOrig:"DEST",sibtSobt:"1234443222",CallSign:"443"}] 

请帮助

链接

谢谢

解决方案

检查以下工作代码...

  const newData = [];realData.forEach((val)=> {让obj = {};val.forEach(v => obj [v.key] = v.value);newData.push(obj);});console.log(newData); 

Im new to javaScript and react..

i have an array consisting of objects. I need to display this array in a AG GRID so what im tring here to map this to array of flat obj.

NOTE: I cannot use jQuery..

below is my array (full code is in codepen, please check the link)

let realData = [
  [
    {
      "key" : "flightNumber" ,
      "label": "Flight Number",
      "value": "EK 131",
      "value-type": "String",
      "order" : 1
    },
    {        
      "key" : "flightId",
      "label": "flightId",
      "value": "1223434",
      "value-type": "String",
      "order" : 2,
      "isId": true
    },
    {

      "key": "CallSign",
      "label": "callSign",
      "value": "123",
      "value-type": "String",
      "order" : 3
    },
  ]
]

what i want the result is

let newArray = [
    {flightNumber: 'EK 131', flightType: 'A', lastUpdate: '11223232', destOrig: 'BEY', sibtSobt: '12121333123', CallSign: '123'},
    {flightNumber: 'EK 132', flightType: 'D', lastUpdate: '11334455', destOrig: 'DEST', sibtSobt: '1234443222', CallSign: '443'}
]

please help

link

Thank you

解决方案

Check the below working code...

const newData = [];

realData.forEach((val) => {
  let obj = {};
  val.forEach(v => obj[v.key] = v.value);
  newData.push(obj);
});

console.log(newData);

这篇关于如何将数组的数组映射到诸如属性和值之类的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆