最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to convert two arrays into an object in JavaScript? - Stack Overflow

programmeradmin1浏览0评论

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 !

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 !

Share Improve this question edited Jul 26, 2021 at 12:06 Mipix asked Jul 26, 2021 at 12:02 MipixMipix 1015 bronze badges 1
  • Yes, it is possible. – Scott Hunter Commented Jul 26, 2021 at 12:04
Add a comment  | 

8 Answers 8

Reset to default 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.

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)

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);

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)

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))

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);

The easiest way is to do this to my mind

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

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);
发布评论

评论列表(0)

  1. 暂无评论