0

I have an array such as var arr = ["Dog", "Cat", "Horse", "Pig", "Cow"]; and I would like to be able to loop through the array to create an object from each element.

var zoo = {
Dog : {
    color: brown,
    age: 4
},
Cat : {
    color: black,
    age: 12

I have found some solutions for defining the other properties with .keys and .values but I am stuck on how to name the object. Thanks for any help!

2
  • 2
    Where do you get values from? I mean color and age for each key. Commented Jan 11, 2019 at 6:11
  • What have you tried? Can you share the rest of the data you want to join with your array? Commented Jan 11, 2019 at 6:12

6 Answers 6

2

You can achieve the solution using Array.reduce. reduce provides an elegant way to return desired output based on your array.

var arr = ["Dog", "Cat", "Horse", "Pig", "Cow"];

var zoo=arr.reduce((zooObj, animal)=>
  {
    zooObj[animal] = {
        color: 'black', //You can add color based on some logic here
        age: 4 //You can add age based on some logic here
    }
    return zooObj
  }, {})

console.log(zoo) //*Output*: { "Dog": { "color": "black", "age": 4 }, "Cat": { "color": "black", "age": 4 }, "Horse": { "color": "black", "age": 4 }, "Pig": { "color": "black", "age": 4 }, "Cow": { "color": "black", "age": 4 } }

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

1 Comment

This works beautifully thank you! I need to get my head around the syntax, but that is exactly what I needed.
1

You can try with below:

var arr = ["Dog", "Cat", "Horse", "Pig", "Cow"];
var colors = ["Red", "Blue", "Green", "Dark", "Yellow"];
var zoo={};
var i = 10;
var colorIndex = 0;
arr.forEach(v=>{
 zoo[v]={};
 zoo[v].age=i++;
 zoo[v].color=colors[colorIndex++];
});
console.log(zoo);

Comments

0

You can simply do it in this way :

var objArr = {};
arr.forEach((data, index) => { 

objArr[data] = data; //here you can add what ever value you want to add to that key

})

Comments

0

var arr = ["Dog", "Cat", "Horse", "Pig", "Cow"];
var zoo = {};
for (var i of arr ){
  zoo[i] = {};
  zoo[i].age = 'age'; //fill the age
  zoo[i].color= 'your favorite color'; //fill with your favorite color
}
console.log(zoo);

Comments

0

This will do-

var animal = ["Dog", "Cat", "Horse", "Pig", "Cow"];
var color=["red","green","blue","orange","yellow"];
var age=[2,5,8,6,23];
var obj={};
 
animal.forEach((e)=>{
    obj[e]={};
    obj[e].age=age[animal.indexOf(e)];
    obj[e].color=color[animal.indexOf(e)];
    
    })
    
    console.log(obj)

Comments

0

You can do this by using below logic. Here define color and age array and inside loop store this dynamically.

var arr = ["Dog", "Cat", "Horse", "Pig", "Cow"];
var colors = ["brown","blak","white","red","gray"];
var ages = ["4","12","5","10","15"];
arr2 = {}
for(var i=0;i<arr.length;i++){
arr2[arr[i]] = {};
arr2[arr[i]].color = colors[i]; //fill the age
arr2[arr[i]].age= ages[i];
}
console.log(arr2);

https://jsfiddle.net/brahmpragya/w76xjm5y/29/

Comments

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.