0

I have an array inside an Object which looks like this.

sourceSystemArray = [{
  "attId" : 2257,
  "attributeName" : "country",
  "attributeValues" : [ "AU", "KG", "IN", "AF" ]
}]

Using input field I give user option to add a new Value. Now I would like to finally add the New Input value that I get using ngModel to attributeValues array. So suppose user enters a new Country say NZ. So I want to push NZ to attributeValues and my final Object should like this:

sourceSystemArray = [{
      "attId" : 2257,
      "attributeName" : "country",
      "attributeValues" : [ "AU", "KG", "IN", "AF","NZ" ]
    }]

I tried using push method but it's not working. Can someone help me figure out how to achieve it.

4
  • 1
    Do you means to push value into attributeValues? Commented Mar 21, 2020 at 7:08
  • What do u want to do? Not clear explain. Commented Mar 21, 2020 at 7:09
  • ???[0].attributeValues.push(something) ... where ??? is the name of the variable that contains that Array you posted Commented Mar 21, 2020 at 7:10
  • If u create demo in StackBlitz we can find better solution Commented Mar 21, 2020 at 7:18

3 Answers 3

1

If you're trying to add this object you might be having problems because it's in an array itself. Here's how I would add to attributeValues.

let myarray = [{
  "attId" : 2257,
  "attributeName" : "country",
  "attributeValues" : [ "AU", "KG", "IN", "AF" ]
}]

myarray[0].attribute values.push('GB')

Or, assuming it's not the only item in the array.

let myarray = [{
  "attId" : 2257,
  "attributeName" : "country",
  "attributeValues" : [ "AU", "KG", "IN", "AF" ]
}]

myarray.find(item => item.attId === 2257)
  .attributeValues.push('GB')
Sign up to request clarification or add additional context in comments.

Comments

0

Since sourceSystemArray is an array, try this

sourceSystemArra[0].attributeValues.push("NZ");

Comments

0

in html part u should send both attId and your new value with function

then in component u should find method with attId

function(attId,newValue){    
    this.sourceSystemArray.find(data=>data.attId ==attId).attributeValues.push(newValue);  
}

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.