0

I have read SO and others but more complex For referrence:

let newpage = {
  page_no_3: {
    data1: [{index: 1, text: "test1"}],
    data2: [{index: 1, text: "test2"}],
  }
}

What I want to happen when I add new object:

let newpage = {
  page_no_3: {
    data1: [{index: 1, text: "test2"}, {index: 2, text: "test3"}, ...]
    data2: [{index: 1, text: "test2"}, {index: 2, text: "test4"}, ...]
  }
}

NOTE: Sorry it should not be an array but an object.

NOTE2: Sorry again. it should be Array with objects as elements.

PS: When I try to get the data of data1 before inserting, it is giving me an undefined error. I tried it like this:

let page_no = 'page_no_3'
if (nextpage.length == 0) {
    ///
} else { // Both undefined
    console.log(nextpage[page_no].data1)
    console.log(nextpage["page_no_3"].data1)
}

But if I only console.log(nextpage[page_no]) I can see the {index: 1, text: "test2"}.

rod james
  • 369
  • 3
  • 13
  • `data 1` has a space in it. Are you sure that's correct? – slebetman Jun 23 '21 at 07:22
  • What do you mean by "add `array1[2]1 in data 1`? Do you want to convert data 1 into an array? Also, you cannot have spaces in keys in your object, unless you enclosed them in quotes. – Terry Jun 23 '21 at 07:22
  • 1
    Can you please show a real reference, the pseudo-reference is not accurate enough. – Teemu Jun 23 '21 at 07:25
  • If I understand correctly what you want probably this `newpage["page_no_3"].data1 = array1[2]` works for you – Alireza Ahmadi Jun 23 '21 at 07:29
  • sorry if I added a space in data and 1. no there are no spaces – rod james Jun 23 '21 at 07:31
  • @Terry No not an array, but data1 should be able to have multiple objects inside. I will try to edit my question – rod james Jun 23 '21 at 07:32
  • Please include some dummy data and show us the expected object you want with your code/logic. See [ask]. – Terry Jun 23 '21 at 07:34
  • How do you plan to use one specific object from those designed by data1 ? With an index ? That would make data1 an array. With a key ? You would need something like `data1 : { "first": {index: 1, text: "test2"}, "second" : {index: 2, text: "test3"},... }` – Alois Christen Jun 23 '21 at 07:51
  • with an index. so an array. – rod james Jun 23 '21 at 07:52
  • Can you please update your question with a complete declaration of the object and how you use it ? The fact that `console.log(nextpage["page_no_3"])` return `{index: 1, text: "test2"}` mean that your structure is like this : `nextpage = { page_no_3:{index:1, text: "test2"}}` . IE page_no_3 doesn't have the data1 property. – Alois Christen Jun 23 '21 at 08:25

1 Answers1

0

First check that your array exist :

if(newpage["page_no_3"].data1 === undefined){
   newpage["page_no_3"].data1 = [];
}

Then you can add a new object to data1 :

newpage["page_no_3"].data1.push(myAwesomeObject);

maybe this can help you to understand the difference between arrays and objects

Alois Christen
  • 543
  • 3
  • 14
  • Thank you for posting an answer. However, I did try `newpage["page_no_3"].data1` and `newpage[page_no].data1` but it is giving me a Cannot read property of undefined. – rod james Jun 23 '21 at 08:03
  • is `newpage` defined ? is `newpage["page_no_3"]` defined ? You need to define everything before using it, newpage and the properties you want to access (here "page_no_3") – Alois Christen Jun 23 '21 at 08:17
  • yes. everything is defined before the if else here. – rod james Jun 23 '21 at 08:18