简体   繁体   English

如何从数组中的数据在对象中创建键?

[英]How to create the key in an object from data inside an array?

How to create the key in an object from data inside an array? 如何从数组中的数据在对象中创建键?

 function createIt(data) { var obj = {}; for (i of data) { obj["fruit"] = i; } return [obj]; } var list = ["orange", "apple", "pineapple"]; console.log(createIt(list)); 
on my code above, the obj.fruit only show the one value, I want to create something like this: 在我上面的代码中, obj.fruit仅显示一个值,我想创建如下内容:

 [ { fruit: "orange" }, { fruit: "apple" }, { fruit: "pineapple" } ] 

Map the input array and get back an array of objects with the key fruit 映射输入数组并使用key果返回对象数组

 var list = ["orange", "apple", "pineapple"]; console.log(list.map(i => ({ fruit: i }))); 

Here a for loop returning a new array, even if I have no idea why you don't want to use map method of arrays: 即使我不知道为什么你不想使用数组的map方法,这里的for循环也会返回一个新数组:

 const list = ["orange", "apple", "banana"]; const newList = []; for (let i = 0; i < list.length; i++) { newList.push({ fruit: list[i] }); } console.log(newList); 

Here a for loop changing the input array, even if again, no idea why you can't use map method of arrays: 这里有一个for循环,即使再次更改输入数组,也不知道为什么不能使用数组的map方法:

 const list = ['orange', 'banana', 'apple']; for (let i = 0; i < list.length; i++) { list.splice(i, 1, { fruit: list[i] }); } console.log(list); 

Back to your original code: 返回原始代码:

function createIt(data){
  var obj = {};
  for(i of data){
    obj["fruit"]= i
  }
  return [obj]
}
var list = ["orange", "apple", "pineapple"];
console.log(createIt(list))

You do create a single object, and you always override the value of the fruit key. 您确实创建了一个对象,并且始终覆盖fruit键的值。 You should create an array, push the objects into the array in the loop, and then return the array, as below: 您应该创建一个数组,在循环中将对象推入该数组,然后返回该数组,如下所示:

 function createIt(data) { var objs = []; for (i of data) { objs.push({fruit: i}); } return objs; } var list = ["orange", "apple", "pineapple"]; console.log(createIt(list)) 

To fix your original code, you should define an initial array, rather than an object, and push an object to that array on each iteration: 要修复原始代码,您应该定义一个初始数组,而不是一个对象,并在每次迭代时将一个对象推入该数组:

 function createIt(data) { const obj = []; for (const i of data) { obj.push({ fruit: i }); } return obj; } var list = ["orange", "apple", "pineapple"]; console.log(createIt(list)) 

That said, .map is much more appropriate when creating a new array based on every item from another array. 也就是说,在基于另一个数组中的每个项目创建新数组时, .map更为合适。

 const createIt = data => data.map(fruit => ({ fruit })); var list = ["orange", "apple", "pineapple"]; console.log(createIt(list)) 

You need an array. 您需要一个数组。 Create the object inside the loop and push the created object to the array in each iteration. 在循环内创建对象,并在每次迭代中将创建的对象推入数组。 Finally return that array: 最后返回该数组:

 function createIt(data){ var arr = []; for(i of data){ var obj = {}; obj["fruit"]= i; arr.push(obj); } return arr } var list = ["orange", "apple", "pineapple"]; console.log(createIt(list)) 

Though you can shorten your code using Array.prototype.map() : 虽然可以使用Array.prototype.map()缩短代码:

The map() method creates a new array with the results of calling a provided function on every element in the calling array. map()方法创建一个新数组,并在调用数组中的每个元素上调用提供的函数。

 function createIt(data){ return data.map(d => ({fruit: d})); } var list = ["orange", "apple", "pineapple"]; console.log(createIt(list)) 

You can simply use .map() to get the desired output: 您可以简单地使用.map()获得所需的输出:

 let list = ["orange", "apple", "pineapple"]; let result = list.map(k => ({fruit: k})); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Just push the object into the array and return it! 只需将对象推入数组并返回它即可!

 'use strict'; function createIt(data) { let res = []; for (const i of data) { res.push({ fruit: i }); } return res; } var list = ["orange", "apple", "pineapple"]; console.log(createIt(list)); 

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

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