0

Given an array in this format:

[
  [{
      name: "name",
      value: "My-name"
    },
    {
      name: "qty",
      value: "1"
    },
    {
      name: "url",
      value: "test.com"
    },
    {
      name: "comment",
      value: "my-comment"
    }
  ],
  [{
      name: "name",
      value: "My-name2"
    },
    {
      name: "qty",
      value: "3"
    },
    {
      name: "url",
      value: "test2.com"
    }
  ],
  [{
      name: "name",
      value: "My-name3"
    },
    {
      name: "qty",
      value: "1"
    },
    {
      name: "url",
      value: "test3.com"
    },
    {
      name: "comment",
      value: "my-comment3"
    }
  ]
]

I'm looking to switch that to:

[
  [
    { name: "My-name" },
    { qty: "1" },
    { url: "test.com" },
    { comment: "my-comment", }
  ],[
    { name: "My-name2" },  
    { qty: "3" },
    { url: "test2.com",
  ],[
    { name: "My-name3", },
    { qty: "1", },
    { url: "test3.com", },
    { comment: "my-comment3", }
  ]
]

In other words, swapping out the array keys but maintaining the object structure within each array element.

I've tried looping over each element and can swap the keys out using something like:

 newArray[iCount][item.name] = item.value;

However I'm then struggling to preserve the object order. Note that the comment field may or may not appear in the object.

5
  • 1
    This datastructure does not make sense to me Commented Oct 24, 2017 at 15:56
  • Note that your output array is missing a } and has a lot of unneeded , which can cause issues in IE. Also it would seem to make a lot more sense to have a single dimension array of objects, instead of a convoluted nested series of arrays, each containing multiple objects Commented Oct 24, 2017 at 15:56
  • What @Jonasw said. I'm willing to be you want a single array of objects rather than an array of arrays of objects. Commented Oct 24, 2017 at 16:00
  • Check my moreBetterOutput value in my answer. i think it may be better for you. Commented Oct 24, 2017 at 16:03
  • The data comes from a serilazied form output. However the inputs need to be split by type hence the funny output Commented Oct 24, 2017 at 16:08

3 Answers 3

1

With Array.map() function:

var arr = [  
   [{name:"name",value:"My-name"},{name:"qty",value:"1"},{name:"url",value:"test.com"},{name:"comment",value:"my-comment"}],
   [{name:"name",value:"My-name2"},{name:"qty",value:"3"},{name:"url",value:"test2.com"}],
   [{name:"name",value:"My-name3"},{name:"qty",value:"1"},{name:"url",value:"test3.com"},{name:"comment",value:"my-comment3"}]
],
result = arr.map(function(a){
    return a.map(function(obj){
        var o = {};
        o[obj.name] = obj.value
        return o;
    });
});
	
console.log(result);

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

Comments

1

Check my moreBetterOutput value. I think will be better.

If you still need a result like your example in the question then you can check output value.

const input = [  
   [  
      {  
         name:"name",
         value:"My-name"
      },
      {  
         name:"qty",
         value:"1"
      },
      {  
         name:"url",
         value:"test.com"
      },
      {  
         name:"comment",
         value:"my-comment"
      }
   ],
   [  
      {  
         name:"name",
         value:"My-name2"
      },
      {  
         name:"qty",
         value:"3"
      },
      {  
         name:"url",
         value:"test2.com"
      }
   ],
   [  
      {  
         name:"name",
         value:"My-name3"
      },
      {  
         name:"qty",
         value:"1"
      },
      {  
         name:"url",
         value:"test3.com"
      },
      {  
         name:"comment",
         value:"my-comment3"
      }
   ]
]

const output = input.map(arr => arr.map(obj => ({[obj.name]: obj.value})))

const moreBetterOutput = output.map(arr => arr.reduce((acc, item, index) => {
    acc[Object.keys(item)[0]] = item[Object.keys(item)[0]];
    return acc;
}, {}) )
//console.log(output);
console.log(moreBetterOutput);

1 Comment

@Jonasw because input is array of arrays.
0

Another map function:

const result = array.map( subarray =>
 Object.assign(...subarray.map( ({name, value}) => ({ [name] : value }) ))
);

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.