0

I was wondering how can I use array push to add values for DeliveryArea. All values will come from php variables. I am beginner in using JS and want to learn and invade js.

var Category = {
    "Status": ["Unpaid", "Paid", "Pending"],
    "OrderDate": ["123", "123", "123"],
    "DeliveryArea": [],
}
2
  • You can access the DeliveryArea array using the dot notation and use array#push method to add new value to it. Commented Oct 29, 2018 at 6:54
  • Use Category.DeliveryArea.push("value") Commented Oct 29, 2018 at 6:54

1 Answer 1

1

Use object name along with key name like this Category.DeliveryArea

var Category = {

  "Status": ["Unpaid", "Paid", "Pending"],
  "OrderDate": ["123", "123", "123"],
  "DeliveryArea": [],
}

let arr = ['arr1', 'arr2', 'arr3'];

arr.forEach(function(item) {
  Category.DeliveryArea.push(item)

})

console.log(Category)

Use square bracket if you wish to access the key through a variable

var Category = {
  "Status": ["Unpaid", "Paid", "Pending"],
  "OrderDate": ["123", "123", "123"],
  "DeliveryArea": [],
}

let keyName = 'DeliveryArea'

let arr = ['arr1', 'arr2', 'arr3'];

arr.forEach(function(item) {
  Category[keyName].push(item)

})

console.log(Category)

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

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.