1

I have a class definition…

class anObj { 
  "ID" : string;
  dialog: {[id : number]:{hide: boolean;}} = {
    0 : {"hide": false},
    14 : {"hide": false}
  }    
}
class manyObjects {
  myGroup: anObj [] = [];
}
...
public stuff = manyObjects;

This totally works just the way I'd like for it to...I can use the id value as a direct key...

var x:number = 1 //used for a different tier of logic
stuff.myGroup[x].dialog[14].hide!=true

Here's where I'm stuck... I'd like to add more dialogs to the group. I get all the way our to...

stuff.myGroup[x].dialog

and can't figure out how to add something like with a push...

.push(7 : {"hide": true})

for example, I can type this line and the IDE says it's ok...

stuff.myGroup[x].dialog[[id=7].push({"hide": false})];

however, when I check, the element does not get added to the array...

4
  • .push() modifies the original array, and doesn't return the new array. Commented Jan 14, 2020 at 6:59
  • yes, Sunny Patel, I want to add a new item to the existing array. I'm not attempting to return it yet. How would you add a new item to it? say one that looks like this... 7 : {"hide": true} Commented Jan 14, 2020 at 7:31
  • I'm not adding a new dialog. I'm building what I now call an asymmetrical key/value pair index array, eg myThing: {[myKey: type] :{key1: type; key2: type}} = {"salutation" : {"key1": "Hi"}, {"key2": "How are you?"}, "conversation" : {"key1": "'s good"}, {"key2": "How about dem Mets?"}} This works if you fully declare ALL elements, eg myThing[salutation].key2 == "How are you?". Makes it REALLY easy to find a specific value without looping all keys by an index. However! I want to add a new element, like "departure" : {"key1": "It's been good."}, {"key2": "see ya!"} and can't. Commented Jan 14, 2020 at 15:54
  • There is no JSON here. JSON is a text format for moving and storing data. See There is no such thing as a JSON Object. Commented Jan 14, 2020 at 16:29

1 Answer 1

1

What I could gather from your code is that you're trying to add a new dialog to the object contained in the "dialog" property of anObj, which is not an array. It's an object with an expected structure in typescript: every property of that object should be a number, and the value of every property should be of type {hide: boolean;}.

From there, it's quite easy, you add a new property (or overwrite an existing one) as you do for any JS object:

stuff.myGroup[x].dialog[7] = {hide: false};

Again, stuff.myGroup[x].dialog is an object, not an array, the array is stuff.myGroup. If you want to add another "group" to myGroup then you'd do something like:

stuff.myGroup.push(new anObj());

EDIT

Example that ignores the noise created by extra objects like stuff and my group, but demonstrates adding a new key-value pair:

class anObj { 
  "ID" : string;
  dialog: {[id : number]:{hide: boolean;}} = {
    0 : {"hide": false},
    14 : {"hide": false}
  }    
}
class manyObjects {
  myGroup: anObj [] = [];
}

var obj = new anObj();
obj.dialog[7] = { hide: true };
console.log(obj);

You can try that in typescript playground -> https://www.typescriptlang.org/play/?ssl=14&ssc=18&pln=1&pc=1#code/MYGwhgzhAEYHYHkBGAraBvaAoa0BEAkgCJ7QBc0EALgE4CWcA5gNw7QAmdYIA9oxegDadduWhwArgFskAUxoBdMugAWI2RSQ8eIWfGYBfA9AC8GNrgAMY9HjXtZeCgDNuEWQYA0F6AEYALDZ26k7QriDuBmzGuNhRoJAwUvAAnsgossBUMOhsUikA4jQ8EgAOFPDp0IIKptUKrFFYAG5gNNA8qHVwsgDusIioABQAlKydKAB0nNx8ggDstWaY9hrQtBKy0AaswDxwEDqyk7yMQxNjQA

Update 2

To return the enumerable keys of an object, you can use the Object.keys method. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

console.log(Object.Keys(obj));
Sign up to request clarification or add additional context in comments.

5 Comments

I'm not adding a new dialog. I'm building what I now call an asymmetrical key/value pair index array, eg myThing: {[myKey: type] :{key1: type; key2: type}} = {"salutation" : {"key1": "Hi"}, {"key2": "How are you?"}, "conversation" : {"key1": "'s good"}, {"key2": "How about dem Mets?"}} This works if you fully declare ALL elements, eg myThing[salutation].key2 == "How are you?". Makes it REALLY easy to find a specific value without looping all keys by an index. However! I want to add a new element, like "departure" : {"key1": "It's been good."}, {"key2": "see ya!"} and can't.
You can call it whatever you want, in javascript that's going to still be a plain object. Now the question is what's preventing you from adding a new key value pair: a typescript compilation error, or a runtime error? Something else? And I have to repeat that you need to take care where and how you add it. The first example I've posted covers the key-value pair, where key is 7 and value is the object {hide: false}
Very cool! I get it. So... if I understand you correctly... if I declare in this way... keyName: {[id : (number or string)] : {key1: type; key2: type}}; in order to add any new element, I don't use push() at all, instead I just treat the new element as if it already exists, and set the value like this... dialog[7] = { hide: true, other: 6} … and then in the future instead of looping through keys looking for 7, I can just directly reference it, var i = 7; if(dialog[i]!=undefined) { //do something with dialog[7].hide} …like that saves around 30 lines of code and a ton of potential bugs! THANKS!
Quick follow-up question... how can you determine the count of dialogs declared for the obj or iterate through them if it's not an array?
Updated with the answer to your follow-up question.

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.