简体   繁体   English

js:使用 reduce() 用数组元素填充对象

[英]js: fill object with array elements using reduce()

trying to learn reduce() but can't understand it well yet.试图学习 reduce() 但还不能很好地理解它。 Maybe someone from you could help me with my problem.也许你的某个人可以帮助我解决我的问题。

I have an object with defined keys and an array.我有一个带有定义键和数组的对象。 I would like to fill up the objects keys with the arrays values using reduce().我想使用 reduce() 用数组值填充对象键。

My sandbox LINK我的沙盒LINK

Till now I tried something like this:直到现在我尝试了这样的事情:

const myObj = {
  first: null,
  second: null,
  third: null
}

const myArr = [ "abc", "def", "ghi"]

const newObj = myArr.reduce((result, value, index) => {
  result[index] = value
  return result
}, myObj)

console.log(newObj)
// 0: "abc"
// 1: "def"
// 2: "ghi"
// first: null
// second: null
// third: null

expected result:预期结果:

{
  first: "abc",
  second: "def",
  third: "ghi"
}

Thanks for any help.谢谢你的帮助。

You need to change index into "an object key at index ":您需要将index更改为“ index处的对象键”:

const newObj = myArr.reduce((result, value, index) => {
    result[Object.keys(myObj)[index]] = value
    return result
}, myObj)

That said, zip + Object.fromEntries would be more appropriate for the job:也就是说, zip + Object.fromEntries更适合这项工作:

 const zip = (...args) => args[0].map((_, i) => args.map(a => a[i])); const myObj = { first: null, second: null, third: null } const myArr = [ "abc", "def", "ghi"] const result = Object.fromEntries( zip( Object.keys(myObj), myArr ) ) console.log(result)

Run through Object.keys and reassign the values for each key from myArr .运行Object.keys并从myArr重新分配每个键的值。

 const myObj = { first: null, second: null, third: null }; const myArr = [ "abc", "def", "ghi"]; Object.keys(myObj).forEach((k,i)=>myObj[k]=myArr[i]); console.log(myObj);

Using switch to determine which object key to assign value to:使用switch来确定将值分配给哪个对象key

 const myArr = ["abc", "def", "ghi"]; const newObj = myArr.reduce((result, value, index) => { let key; switch (index) { case 0: key = "first"; break; case 1: key = "second"; break; case 2: key = "third"; break; } result[key] = value; return result; }, {}); console.log(newObj);

OR, for a more flexible option that does not rely on the descriptive key word:或者,对于不依赖于描述性关键字的更灵活的选项:

 const myArr = ["abc", "def", "ghi", "jkl", "mno"]; const newObj = myArr.reduce((result, value, index) => { result[(index + 1).toString()] = value; return result; }, {}); console.log(newObj); /* { "1": "abc", "2": "def", "3": "ghi", "4": "jkl", "5": "mno" } */


Here's a SO solution that converts an integer to an ordinal , ie, 1 to "first", 2 to "second", etc.—up to "ninety-ninth".这是一个将整数转换为序数的 SO 解决方案,即将 1 转换为“第一”,将 2 转换为“第二”,等等——直到“第九十九”。 That gives you ordinal keys, and eliminates the need for the object myObj .这为您提供了序号键,并消除了对对象myObj的需要。 By depending on myObj to provide the key names you'll need to predetermine how many elements are in myArr , and be sure there's a key in myObj for each.通过依赖myObj提供键名,您需要预先确定myArr中有多少元素,并确保myObj中每个元素都有一个键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM