1

Is it possible to create the the data1 array without using nested for loops?

// My starting Normalized data
var fields = ["name","age"];
var data2 = [["John",20],["Tom",25]]; 


// What I want the result to look like Denormalized
var data1 = [{"name":"John", "age":20},{"name":"Tom", "age":25}];


// My solution
var data1 = [];
for(var i = 0; i < data2.length; i++){
   var temp = {};
   for(var y = 0; y < fields.length; y++){
      temp[fields[y]] = data2[i][y];
   }
   data1.push(temp);
}

5 Answers 5

1

You can use map and forEach

var fields = ["name","age"];
var data2 = [["John",20],["Tom",25]]; 
var data1 = data2.map(function(arr){ 
   var obj = {};
   arr.forEach(function(val, ind){ obj[fields[ind]] = val; });
   return obj;
});

but it is basically nest loops.

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

1 Comment

check out my answer, it doesn't use nested for loops!
0

Or you could start getting acquainted with Javascript tools like the underscore/lodash libraries that offer a lot of utilities functions for cases like this.

For example, using _.zipObject() offered by lodash:

fields = ["name", "age"];
data2 = [["John", 20],["Tom", 25]];
res = [];
data2.forEach(function(arr) {
    res.push(_.zipObject(fields, arr));
});

In essence as @epascarello mentioned, you are still doing a double loop. It's just more elegant (subject always to coding taste) and more compact.

Comments

0

No loops...

var data1 = [];
data1.push(eval('({"' + fields[0] + '":"' + data2[0][0] +
                '","' + fields[1] + '":' + data2[0][1] + '})'));
data1.push(eval('({"' + fields[0] + '":"' + data2[1][0] +
                '","' + fields[1] + '":' + data2[1][1] + '})'));

Guess it depends on your definition of efficient.

2 Comments

without a loop of some kind, it has to look similar to this.
i just didn't want nested for loops, I posted the answer. thank you for your input!
0

Another implementation using native map and reduce (which will be nested loops - but figured I'd throw it in as another option):

var data1 = data2.map(function(currentArray, index){
  return currentArray.reduce(function(objToReturn, currentValue, index){
    objToReturn[fields[index]] = currentValue;
    return objToReturn;
  },{});
});

Comments

0

I finally thought of an efficient way that doesn't use nest for loops! :)

var fields = ["name","age"];
var data2 = [["John",20],["Tom",25]]; 
var body = "";

for(var i = 0; i < fields.length; i++){
   body = body.concat("this."+fields[i] +"=args["+i+"]; ");
}

var model = new Function("args",body);

var data1 = [];
for(var i = 0; i < data2.length; i++){
   var x = new model(data2[i]);
   data1.push(x);
}

2 Comments

And it is really fragile...The most efficient way would be to do it on the server to start!
I bet this is slower than two loops and less efficient. new Function is not "cheap".

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.