Taylor
Taylor

Reputation: 57

Array push in array in js to a variable from php

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": [],
}

Upvotes: 0

Views: 94

Answers (1)

brk
brk

Reputation: 50291

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)

Upvotes: 1

Related Questions