1

I have two arrays consist of strings ["25","36","32"] and ["GradeA", "GradeB", "GradeC"]

I want to merge them all together and want to produce JSON string something like this:

{"GradeA" : "25", "GradeB" : "36", "GradeC": "32"}

How do I do this in js?

1

6 Answers 6

1

And for an extra 2 cents I give you the forEach loop version of it. This is taking into consideration that each array is going to be 1 for 1 and in the appropriate order.

var grade = ["GradeA", "GradeB", "GradeC"];
var number = ["25","36","32"];
var obj = {};

grade.forEach(function(x, i){
    obj[x] = number[i];
});

console.log(JSON.stringify(obj));
Sign up to request clarification or add additional context in comments.

Comments

0

This below code will give your expected output:

 var arr1 = [25,36,32] ;
 var arr2 =  ["GradeA", "GradeB", "GradeC"];
 var gradeObj = {};
 for(var i = 0;i<arr2.length;i++){
   gradeObj[arr2[i]] = arr1[i];
 }
 
 console.log(JSON.stringify(gradeObj));

Comments

0

Try like this

var keys =["GradeA", "GradeB", "GradeC"];
var values= ["25","36","32"];
var obj = {};
for(var i=0; i< keys.length; i++){
    if(values.length>i+1){
         obj[keys[i]] = values[i];
     }else{
         obj[keys[i]] = null;
     }
} 
console.log(obj);

Comments

0

Using ES6 reduce:

{
  const a = ["25","36","32"];
  const b = ["GradeA", "GradeB", "GradeC"];
  const merge = (keys, values) => keys.reduce(
    (obj, key, i) => (obj[key] = values[i], obj),
    {}
  );

  const json = JSON.stringify(
    merge(b, a)
  );
  console.log(json);
}

with native Reflect API:

{
  const a = ["25","36","32"];
  const b = ["GradeA", "GradeB", "GradeC"];
  const merge = (keys, values) => keys.reduce(
    (obj, key, i) => Reflect.set(obj, key, values[i]) && obj,
    {}
  );

  const json = JSON.stringify(
    merge(b, a)
  );
  console.log(json);
}

Comments

0

Using Array#reduce and Object#assign to add a property and it's value to the object on each iteration:

const keys = ["GradeA", "GradeB", "GradeC"];
const values = ["25","36","32"];

const result = keys.reduce((obj, key, i) => Object.assign(obj, { [key]: values[i] }), {});

console.log(result);

Using Array#map, to create a series of object with one property each, then combining them to a single object with Object#assign, and spread:

const keys = ["GradeA", "GradeB", "GradeC"];
const values = ["25","36","32"];

const result = Object.assign(...keys.map((key, i) => ({ [key]: values[i] })));

console.log(result);

Comments

0

Try ES6 Destructuring Assignment :

var obj = {};

var arr1 = ["25","36","32"];
var arr2 = ["GradeA", "GradeB", "GradeC"];

function f([...val1], [...val2]) {
    for (var i in val1) {
        obj[val1[i]] = val2[i];
    }
}

f(arr2, arr1);

console.log(obj);

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.