6

I have two arrays, one for the players names :

players_name = ['John','Paul','Jack',...]

And another for the points of the players :

points = [23,13,18,...]

And I would like to know if it was possible to (not manually) transform this two arrays into an object that would look like this :

players_results = {
    'John': { point: 23},
    'Paul': { point: 13},
    'Jack': { point: 18},
}

Thanks !

1
  • Yes, it is possible. Commented Jul 26, 2021 at 12:04

8 Answers 8

12

You can use Object.fromEntries() for achieving your required result.

fromEntries takes an array of arrays (the nested array contains two values, 1st one is the key and the 2nd one is the value) and then converts it to an object. Try this-

const players_name = ['John','Paul','Jack'];
const points = [23,13,18];

const result = Object.fromEntries(
  players_name.map((name, i) => ([name, {point: points[i]}]))
);

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

Note: This is not a good idea to use a name as the key of an object. Names might be similar for different people. Use a unique value as a key instead.

0
6

const players_name = ['John', 'Paul', 'Jack']
const points = [23, 13, 18]

const result = players_name.reduce((prev, item, i) => {
  prev[item] = {
    point: points[i]
  }
  return prev;
}, {});

console.log(result)

2

const players_name = ['John','Paul','Jack'];
    
const points = [23,13,18];

const players_results = {}

players_name.forEach((data, index) => {
  players_results[data] = {
      point: points[index]
    }
});

console.log(players_results);

2

You could do with Array#forEach

const players_name = ['John','Paul','Jack']
const points = [23,13,18]

players_results ={};

players_name.forEach((a,i)=>{
  players_results[a] = {point:points[i]}
})

console.log(players_results)

1

I don't know if you consider this is manually or not but you can use this function:

let players_name = ['John','Paul','Jack'];

let points = [23,13,18];

function tabToObject(tab1, tab2) {
  let lengthMin = Math.min(tab1.length, tab2.length);
  let returnObject = new Object();
  for (let i = 0; i < lengthMin; i++) {
    returnObject[tab1[i]] = {point: tab2[i]};
  }
  return returnObject;
}

console.log(tabToObject(players_name, points))

1

You can use reduce array method Array.prototype.reduce()

const players_name = ['John','Paul','Jack'];
const points = [23,13,18];

const object = players_name.reduce((acc, cur, index) => {
  acc[cur] = {points: points[index]};
  return acc;
}, {});
console.log(object);

0

The easiest way is to do this to my mind

players_name.forEach((name, index) => {
    players_results[name] = {point: points[index]}
})
0

Square bracket notation is a very helpful syntax that can help get out of nasty situation with ease when using JavaScript Objects.

var players_name = ['John','Paul','Jack'];
var points = [23,13,18];

function arrayToObject(arr1,arr2){
  obj = {};
  for(i in arr1){;
    obj[arr1[i]] = arr2[i];
  }
  return obj;
}

x = arrayToObject(players_name, points)
console.log(x);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.