2

How to turn this: [[a1, b1], [a2, b2]] into this? [{x1: a1, y2: b1}, {x2: a2, y2: b2}]?

Note: the x1 and x2 are newly defined properties (with a1, b1, etc. as values).

Note: This is what I tried:

formatData.map((data, index) => {
  const obj = {
    dataX: data[index][0],
    dataY: data[index][2]
  }
  data = obj
}

But became stuck.

8
  • look up the .map() method of array Commented Nov 23, 2017 at 7:41
  • 1
    I'm voting to close this question as off-topic because the question does not show any sign of effort. This is a requirement and not a problem statement. Commented Nov 23, 2017 at 7:45
  • 1
    StackOverflow expects you to try to solve your own problem first, and we also don't answer homework questions. Please update your question to show what you have already tried in a minimal, complete, and verifiable example. For further information, please see how to ask good questions, and take the tour of the site :) Commented Nov 23, 2017 at 7:46
  • 1
    please add a valid result with given data. Commented Nov 23, 2017 at 7:51
  • Just a pointer, using a map where you do not respect its output or do not return anything from callback is a BAD implementation. Commented Nov 23, 2017 at 7:53

4 Answers 4

3

Simple for loop can do this

var arr = [["a1", "b1"], ["a2", "b2"]];
var newArr = [];
var repeater = ['x','y','z'];//...
for(var i in arr){
  var obj = {};
  for(var j in arr[i]){  
    obj[repeater[j]+(parseInt(i)+1)] = arr[i][j];
  }
  newArr.push(obj);
}
console.log(newArr);

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

4 Comments

Please do not answer such question. Let OP share effort. Don't promote spoon feeding
@Rajesh : I was only able to anwer. I Didn't comment to say anything, because of lower reputation, but next time I will do this. Thanks
The expectation is key with x1 & y1 and so no , but in your case the keys are x0,x1...
@brk : I have updated my answer : Please share you comment if you feel its good answer.
1

You can use .map of array as below

var temp = [['a1', 'b1'], ['a2', 'b2']];

temp.map(function(arr,index){
   var ret = {};
   ret['x'+(index+1)]= arr[0];
   ret['y'+(index+1)]= arr[1];
   return ret;
});

//"[{"x1":"a1","y1":"b1"},{"x2":"a2","y2":"b2"}]"

5 Comments

Please let OP to share effort. Answering such question promotes spoon feeding
Agreed we should not do spoon feeding, I guess OP added efforts now.
Yes he did now. But the point is, should we not wait to see if OP has tried something and then provide solution? In my POV, SO is to solve problem statements and not requirements.
Yep, will take care of that next time.
What if I have 3 values? And I don't want the middle one in the result? var data = [[‘a1’, ‘b1’, ‘c1’], [‘a2’, ‘b2’, ‘c2’]] // => x1: a1, y1: c1 no b1 value
1

Use forEach

var m = [
  ['a1', 'b1'],
  ['a2', 'b2']
];
var tempArray = []
m.forEach(function(item, index) {
  // create a local object to set the key and value into it
  var localObj = {};
  // setting key and value
  localObj['x' + 1] = item[0];
  localObj['y' + 1] = item[1]
  // push that pbject to an array
  tempArray.push(localObj)
})

console.log(tempArray)

2 Comments

What if I have 3 values? And I don't want the middle one in the result? var data = [[‘a1’, ‘b1’, ‘c1’], [‘a2’, ‘b2’, ‘c2’]] // => x1: a1, y1: c1 no b1 value
in that case you need to maintain an array of keys like [a,b,c,d]
0

You could map the values and take Object.assign for new objects.

var keys = ['x', 'y'],
    data = [['a1', 'b1'], ['a2', 'b2']],
    result = data.map(a => Object.assign(...keys.map((k, i) => ({ [k]: a[i] }))));
    
console.log(result);

With an object for the wanted keys and corresponding indices.

var keys = { x: 0, y: 2 },
    data = [['a1', 'b1', 'c1'], ['a2', 'b2', 'c2']],
    result = data.map(a => Object.assign(...Object
        .keys(keys)
        .map(k => ({ [k]: a[keys[k]] }))
    ));
    
console.log(result);

9 Comments

Please let OP to share effort. Answering such question promotes spoon feeding
@Rajesh, not here [{x1: a1, y2: b1} ....
@Rajesh, once the answer meets and satisfies the desired output I think that't not a problem. Also, OP includes what he tried in answer :)
@Alexandru-IonutMihai OP just added code. And the point is, since we are older users, we should try to enforce and maintain certain discipline. This still is my POV, but I guess we should not promote spoon feeding
@Rajesh, my pov is, if there is already an answe, but it could be better, then i make my answer for it. but i think this q is a dupe.
|

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.