0

I have an object that contains an array. While the items push through, what I am trying to do is set each item upon creation to have a checked value of False. Currently, I am seeing that this is setting the entire array to have a value of false. How can I modify this to have each item it's own checked value?

Desired output of items:

["a", "checked": False], ["aa", "checked": False], ["aaa", "checked": False]

// state objects
var state = {
    items: []
};

// state modification functions
function addItem(state, item) {
    item.name = item;
    state.items[item["checked"]] = "False";
    state.items.push(item);
    console.log(state.items);
}
2
  • 1
    Looks like you really should be using an object instead. Commented Oct 14, 2016 at 22:08
  • 2
    items.push({value: "a", checked : false}) Commented Oct 14, 2016 at 22:10

2 Answers 2

3

What you want is an Array that contains Objects. Try this:

function addItem(state, item) {
    state.items.push({
      name   : item,
      checked: false
    })
}

You can then access state.items[0] to get the { name, checked } object, or state.items[0].name/state.items[0].checked directly

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

Comments

0

Some time easier is witch object

[{name: "a", checked: false}, {name: "aa", checked: false}]

// state objects
var state = {
    items: []
};

// state modification functions
function addItem(item) {
    state.items.push(item);
}

addItem({name: "a", checked: false})

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.