Reputation: 233
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));
obj.fruit
only show the one value,
I want to create something like this:
[
{ fruit: "orange" },
{ fruit: "apple" },
{ fruit: "pineapple" }
]
Upvotes: 1
Views: 64
Reputation: 68933
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()
:
The
map()
method creates a new array with the results of calling a provided function on every element in the calling array.
function createIt(data){
return data.map(d => ({fruit: d}));
}
var list = ["orange", "apple", "pineapple"];
console.log(createIt(list))
Upvotes: 1
Reputation: 9988
Map the input array and get back an array of objects with the key
fruit
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:
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:
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. 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))
Upvotes: 5
Reputation: 5853
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));
Upvotes: 0
Reputation: 370679
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.
const createIt = data => data.map(fruit => ({ fruit }));
var list = ["orange", "apple", "pineapple"];
console.log(createIt(list))
Upvotes: 1
Reputation: 39322
You can simply use .map()
to get the desired output:
let list = ["orange", "apple", "pineapple"];
let result = list.map(k => ({fruit: k}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 0