0

So my goal is to have an object variable that will be empty at the start but as the code starts running it would get filled up with data from other varibales. When it gets filled up it should look like this:

{
    "banana" : 
    {
        "color" : "yellow",
        "calories" : 300
    },
    "apple" : 
    {
        "color" : "green",
        "calories" : 250
    },
    ...
}

The two arrays and one JSON object that are supposed to be the source of information look like this:

var fruits = {
    "product 1" :
    {
        "price" : 1.2,
        "stock" : 124,
        "name" : "banana"
    },
    "product 2" :
    {
        "price" : 0.6,
        "stock" : 21,
        "name" : "apple"
    },
    ...

}

var colors = [yellow, green, ...];
var calories = [300, 250, ...];

To put it simply I need to pull the name of the fruit from the JSON object and assign the appropriate calories and color attributes.

So far I've been trying to make this work for at least an hour now, but failed. Some of my attempts are the following:

 var object.(fruits.product1) = JSON.stringify({colors[0] : calories[0]});

//also tried this
var object.(fruits.product1) = "{""'" + (colors[0]) + "'":+calories[0]+"}";
//and tried many other things...

Also please note that simply dumping it in an array of objects is not an option as this object is going to be huge and finding the desired listing will be extremely wasteful with time and resources that way.

3 Answers 3

1

I believe something like this will suit your needs:

var fruits = {
  "product 1" : {
    "price" : 1.2,
    "stock" : 124,
    "name" : "banana"
  },
  "product 2" : {
    "price" : 0.6,
    "stock" : 21,
    "name" : "apple"
  }
}

var colors = ['yellow', 'green'];
var calories = [300, 250];
var json = {};

Object.keys(fruits).forEach((key, index) => {
  json[fruits[key].name] = {
    color: colors[index],
    calories: calories[index]
  }
})

The object created looks like

{
  "banana" : {
    "color" : "yellow",
    "calories" : 300
  },
  "apple" : {
    "color" : "green",
    "calories" : 250
  },
}
Sign up to request clarification or add additional context in comments.

2 Comments

I get this error: ReferenceError: json is not defined pointing at json[fruits[key].name] = {
My apologies, I forgot to include the defined variables
1
    var fruits = {
    "product 1" :
    {
        "price" : 1.2,
        "stock" : 124,
        "name" : "banana"
    },
    "product 2" :
    {
        "price" : 0.6,
        "stock" : 21,
        "name" : "apple"
    },
}

var colors = ['yellow', 'green']
var calories = [300, 250]

const result = {}
 Object.keys(fruits)
          .forEach((key, index) => {
             Object.assign(result, {
               [fruits[key].name]: {
                 color: colors[index],
                 calories: calories[index],
                 stock: fruits[key].stock,
                 price: fruits[key].price,
               }

             })
           })
console.log(result)

Should display:

{
  "banana": {
    "color": "yellow",
    "calories": 300,
    "stock": 124,
    "price": 1.2
  },
  "apple": {
    "color": "green",
    "calories": 250,
    "stock": 21,
    "price": 0.6
  }
}

Comments

0
var fruits = {
    "product 1" :
    {
        "price" : 1.2,
        "stock" : 124,
        "name" : "banana"
    },
    "product 2" :
    {
        "price" : 0.6,
        "stock" : 21,
        "name" : "apple"
    },
}

var colors = ['yellow', 'green']
var calories = [300, 250]

 Object.keys(fruits)
          .forEach((key, index) => {
             Object.assign(fruits[key], {
                color: colors[index],
                calories: calories[index]
             })
           })

Fruits object after this:

{
  "product 1": {
    "price": 1.2,
    "stock": 124,
    "name": "banana",
    "color": "yellow",
    "calories": 300
  },
  "product 2": {
    "price": 0.6,
    "stock": 21,
    "name": "apple",
    "color": "green",
    "calories": 250
  }
}

1 Comment

This is similar to what I need, but it's crucial that the name is the parent and not an attribute (child), otherwise it's gonna be very time inefficient to look for certain listings.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.