0

Object attribute only hold the first element of the assigned value

let groupedDepActivities=[]
 groupedDepActivities.push({
            id:1,
            term_activity:{
              terms:[{id:1},{from:'here'},{to:'there'},]
            }
          })

the console.log() result will be * term_activity: terms: Array(1) 0: id: "1" [[Prototype]]: Object length: 1 * terms attribute only hold the first element(id:1) of the array not all

1
  • 1
    This worked ok for me. Ran this exact code in console. Inspected groupedDepActivities and drilled down terms, its an array with 3 items. Commented Aug 30, 2021 at 22:24

2 Answers 2

1

The console's output may be truncated, but your code works as expected.

let groupedDepActivities = []
groupedDepActivities.push({
  id: 1,
  term_activity: {
    terms: [{
      id: 1
    }, {
      from: 'here'
    }, {
      to: 'there'
    }, ]
  }
})

console.log(groupedDepActivities);

Output:

[
  {
    "id": 1,
    "term_activity": {
      "terms": [
        {
          "id": 1
        },
        {
          "from": "here"
        },
        {
          "to": "there"
        }
      ]
    }
  }
]

Were you wanting terms to be a single object?

let groupedDepActivities = []
groupedDepActivities.push({
  id: 1,
  term_activity: {
    terms: {
      id: 1,
      from: 'here',
      to: 'there',
    }
  }
})

console.log(groupedDepActivities);

[
  {
    "id": 1,
    "term_activity": {
      "terms": {
        "id": 1,
        "from": "here",
        "to": "there"
      }
    }
  }
]
Sign up to request clarification or add additional context in comments.

1 Comment

it worked when I run on the browser'ss console, but it stores the first array element on my vuejs code? what shall I do?
0

you are only pushing one object, this one:

{
    id:1,
    term_activity:{
    terms:[{id:1},{from:'here'},{to:'there'},]
    }
}

Need to distiguish when you are handling an object: {}, from an array []. To dive deeper in your data structure for example you can do: console.log(groupedDepActivities=[0].term_activity.terms[0])

Maybe its also useful to wrap your item to log in braces, as it appears as an object with names in the console, like this: console.log({groupedDepActivities})

enter image description here

In case checking the names of the variables you log while you unfold them to check what do they hold make you more compfortable :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.