1

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:

[
  { fruit: "orange" },
  { fruit: "apple" },
  { fruit: "pineapple" }
]
2
  • You could use map method var res = list.map(fruit => ({fruit})) Commented Jan 9, 2019 at 12:19
  • you're overriding obj["fruit"]. Add to an array in each iteration instead. Commented Jan 9, 2019 at 12:20

5 Answers 5

5

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

Sign up to request clarification or add additional context in comments.

7 Comments

can we just use for-loop for this case?? :D
if you want, we can. Do you want to mutate the input list, or do you want to return a new array?
@ZumDummi Why would you prefer a for loop over a higher order function like .map() ?
can we do both?:D
@ZumDummi Uhm... he was just trying to be helpful and even updated his answer.
|
1

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

Comments

1

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

2 Comments

i like this method:D
@ZumDummi, great to know that:)
0

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

1 Comment

can we just use for-loop for this case?? :D
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));

2 Comments

The code doesn't run in strict mode, but don't forget to define i anyway ;)
@NullDev there fixed it!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.