0

Let's imagine I have this :

var fruits = [{ "Banana" : "yellow", "lemon" : "yellow"}, {"Orange" : "orange", "Mango" : "orange"}, {"Apple" : "red", "strawberry" : "red"}];

fruits.push({"Kiwi" : "green", "Green Apple" : "green", "watermelon" : "green" });

console.log(fruits);

Now I have an Array "fruits", with objects inside, and I'm using "push()" method to add new objects.

If I would like to add a new yellow fruit, like "Jamaican Banana : yellow" inside the yellow things object, how could I do that ?

5
  • you have wrong syntax of objects Commented Mar 17, 2017 at 7:45
  • Your example isn’t syntactically valid. If you meant {"Banana": "yellow", "lemon": "yellow"}… why store them like that? It’s the least useful structure. {"yellow": ["Banana", "lemon"], "orange": …}" would make a lot more sense. Commented Mar 17, 2017 at 7:45
  • Have you tried to run this ? Commented Mar 17, 2017 at 7:46
  • You should use a hash Commented Mar 17, 2017 at 7:46
  • Actually, it's just an random exemple. In my project I'm using a firebase ref to fill up the array using a foreach. Then I have my Array, fill up with objects inside that I transfer to the scope. And I would like to know if it exist a simple way to add something in these objects. Because the problem with the "push" method is, each push create a new object. Wich is an action I want, but I also would like to add something more in each steps of my loop, am I enough clear ? Commented Mar 17, 2017 at 7:50

3 Answers 3

3

What you're trying to do won't work very well. Try something like this:

var fruits = {
  yellow: ["fruit1", "2", "3"],
  orange: ["4","5","6"]
};

// later
console.log(fruits);
fruits.yellow.push("another fruit");
console.log(fruits);

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

Comments

2

You need to quote the keys, if necessary and the string values, but not the wole part with colon inside: Object initializer.

var fruits = [{ Banana: "yellow", lemon: "yellow" }, { Orange: "orange", Mango: "orange" }, { Apple: "red", strawberry: "red" }];

fruits.push({ Kiwi: "green", "Green Apple": "green", watermelon: "green"});

console.log(fruits);

Comments

2

Use dynamic property accesses (bracket notation), and don't forget the quotes that go between property names and : in your other fruit objects:

var fruits = [{
  "Banana": "yellow",
  "Lemon": "yellow"
}, {
  "Orange": "orange",
  "Mango": "orange"
}, {
  "Apple": "red",
  "Strawberry": "red"
}]

fruits.push({
  "Kiwi": "green",
  "Green Apple": "green",
  "Watermelon": "green"
})

fruits[0]["Jamaican Banana"] = "yellow"

console.log(fruits)

However, I would suggest (like Ryan and Mehul Mohan have already) using arrays within a top-level fruits object instead (in other words, sorting your data by color):

var fruits = {
  "yellow": ["Banana", "Lemon"],
  "orange": ["Orange", "Mango"],
  "red": ["Apple", "Strawberry"]
}

fruits["green"] = ["Kiwi", "Green Apple", "Watermelon"]

fruits["yellow"].push("Jamaican Banana")

console.log(fruits)

3 Comments

Oh that's seems to fit to my needs ! How would you do the same in a same Array size loop ? Let's imagine we have "fruits[iteration][fruit] = color", "iteration", "fruit" and "color" as variables.
Just like that: for (var i = 0; i < fruits.length; i++) { fruits[i]["Example Fruit"] = "example-color" }. However, this will mess up the sorting of your data which is why the alternative data structure I suggested is more desirable.
Actually, it's just an random exemple. In my project I'm using a firebase ref to fill up the array using a foreach. Then I have my Array, fill up with objects inside that I transfer to the scope. And I would like to know if it exist a simple way to add something in these objects. Because the problem with the "push" method is, each push create a new object. Wich is an action I want, but I also would like to add something more in each steps of my loop, am I enough clear ?

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.