0

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

5
  • What have you tried ? I recommend you take the tour: stackoverflow.com/tour Commented Feb 26, 2018 at 11:15
  • please see this codepen.io/re5ive/pen/qxJxEq?editors=0012 Commented Feb 26, 2018 at 11:16
  • Please include all important information directly into the question. See the help center for question requirements! Commented Feb 26, 2018 at 11:17
  • There are some keys that are expected to be values? There are value and keys that didn't exist in JSON but do in result? sibtSobt? Commented Feb 26, 2018 at 11:17
  • @zer00ne i cannot put all the data code in the editer.. so i put tham in codepen and attach the link. Commented Feb 26, 2018 at 11:19

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

Comments

3

You could map the new objects with Object.assign for a single object.

var data = [[{ 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 }]],
    result = data.map(a => Object.assign(...a.map(({ key, value }) => ({ [key]: value }))));

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

1 Comment

Thanks for the code. but please see the full array in codepen.
2

You can reduce each array key/value pair into an object :

const arrays = [
[
    {
      "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
    }
]
];

const objects = arrays.map(arr => arr.reduce((acc, cur, i) => {
  acc[cur.key] = cur.value;
  return acc;
}, {}));

console.log(objects);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.