0

I have a large array of json objects, that I need to iterate through and add them to another json array.

The array looks like the following -

{
  id : 1,
  name : "test1",
  location : "London",
  cost: "120.83"
},
{
  id : 2,
  name : "test2",
  location : "shanghai",
  cost: "124.83"
},
{
  id : 3,
  name : "test3",
  location : "Barcelona",
  cost: "56.54"
}

The actual array that im using has 73 key, value pairs, and about 20000 objects.

I am trying to transpose each individual object into another array, that needs to look like -

{
  cells: 
    [
       {
         value: 1,
         textAlign: "center",
         background: "rgb(255,255,255)",
         color: "rgb(0,62,117)"
       },
       {
         value: "test1",
         textAlign: "center",
         background: "rgb(255,255,255)",
         color: "rgb(0,62,117)"
       },
       {
         value: "London",
         textAlign: "center",
         background: "rgb(255,255,255)",
         color: "rgb(0,62,117)"
       },
       {
         value: "120.83",
         textAlign: "center",
         background: "rgb(255,255,255)",
         color: "rgb(0,62,117)"
       },
    ]
},
{
  cells: 
    [
       {
          value: 2,
          textAlign: "center",
          background: "rgb(255,255,255)",
          color: "rgb(0,62,117)"
       },
       {
          value: "test2",
          textAlign: "center",
          background: "rgb(255,255,255)",
          color: "rgb(0,62,117)"
       },
       {
          value: "shanghai",
          textAlign: "center",
          background: "rgb(255,255,255)",
          color: "rgb(0,62,117)"
       },
       {
          value: "124.83",
          textAlign: "center",
          background: "rgb(255,255,255)",
          color: "rgb(0,62,117)"
       },
   ]
},

I just cant get my head around it, it must be the monday fog!

Thanks

4
  • 3
    how do they relate to each other? please add a consitent example. Commented Feb 25, 2019 at 12:28
  • 1
    When giving example input & expected output, make it valid. It stops all the questions later. Looking at your input & output, I've a feeling you have put 1 too many items in the input example. Commented Feb 25, 2019 at 12:29
  • @NinaScholz they are related, op is mapping each value of each object to a element in calls array Commented Feb 25, 2019 at 12:36
  • the actual fields and values are irrelevant for this example, its just the actual way of transposing i am after. Commented Feb 25, 2019 at 12:48

3 Answers 3

1

You can do it with Array.map() and Object.values():

const data = [
  { id : 1, name : "test1", location : "London", cost: "120.83" },
  { id : 2, name : "test2", location : "shanghai", cost: "124.83" },
  { id : 3, name : "test3", location : "Barcelona", cost: "56.54" }
];

const cell = { textAlign: 'center', background: 'rgb(255,255,255)', color: 'rgb(0,62,117)' };

const result = data.map(x =>
  ({ cells: Object.values(x).map(v => ({ value: `${v}`, ...cell })) })
);

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

0

You can use .map() and Object.assign() methods:

const data = [
  {id : 1, name : "test1", location : "London", cost: "120.83"},
  {id : 2, name : "test2", location : "shanghai", cost: "124.83"},
  {id : 3, name : "test3", location : "Barcelona", cost: "56.54"}
];

const obj = {
  textAlign: "center",
  background: "rgb(255,255,255)",
  color: "rgb(0,62,117)"
};

const result = data.map(o => ({
   cells: Object.keys(o).map(k => Object.assign({}, {value: o[k]}, obj))
}));

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

Comments

0

You can use map and reduce

let arr = [{id:1,name:"test1",location:"London",cost:"120.83"},{id:2,name:"test2",location:"shanghai",cost:"124.83"},{id:3,name:"test3",location:"Barcelona",cost:"56.54"}]

let defaultObject = {textAlign: "center",background: "rgb(255,255,255)",color: "rgb(0,62,117)" }

let op = arr.map(e => Object.values(e).reduce((op,inp) => {
    op.cells.push({value: inp, ...defaultObject } )
    return op
  },{cells:[]})
)

console.log(op)

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.