0

Hi I am developing web application in Angular 5. I have one array and I want to update the same array based on the value of another array. For example, I have createarray as below.

enter image description here

I have another object as below.

data:{
checked:true,
name:infomodel
}

Now there is checked true in data. So for infomodel in createarray I want to update the checked property.

I tried as below.

           let copyCreate = Object.assign({}, node.data);
            copyCreate.checked = true;
            const targetIdxCreate = this.createnode.map(item => item.name).indexOf(copyCreate.name);
            this.createnode[targetIdxCreate] = copyCreate;

            let copyUpdate = Object.assign({}, node.data);
            copyUpdate.checked = false;
            const targetIdxUpdate = this.updatenode.map(item => item.name).indexOf(copyUpdate.name);
            this.updatenode[targetIdxUpdate] = copyUpdate;

In the above code, It updates createnode checked with true. Also I am making updatenode chcked to false. After executing the above code, both arrays will have checked property false.

can someone help me to do this?

1
  • your createnode array has "Info model" and your another object has "infomodel".....find() will return nothing. Commented Sep 14, 2018 at 7:38

3 Answers 3

2

I would suggest to add the check based on 'id' field that you have in your record that you want to update with. In that case the ideal way to update the record would like like below:

// Assuming updateWith is the object that you want to update with
const targetIdx = this.createnode.map(item => item.id).indexOf(this.updateWith.id);
this.createnode[targetIdx] = this.updateWith;

if you still prefer to use 'name' as a parameter that you want to check the condition for, then you can just change the id to name in the map function like so:

// Assuming updateWith is the object that you want to update with
const targetIdx = this.createnode.map(item => item.name).indexOf(this.updateWith.name);
this.createnode[targetIdx] = this.updateWith;

Hope this solves your problem :)

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the answer but I dont have ID filed in createarray but i have it in other array.
Then you can go with the second option, if that case suits you. Either way the logic for replacing is same. Just the parameter with with you want to choose to compare with is upto your usecase :)
Your answer works fine. But I have updated my code above. Whenever I update other array updatenode then both arrays will have false, But createnode array supposed to have true and updatenode array supposed to have false.
What is 'node.data'? Is it a global value or some thing? The problem that i feel happening here is when you update node.data object for the second time it is changing it's values in the previous object with which it was mapped to. In order to prevent this condition you may have to copy the 'node.data' properties to another object before making the new update 'Object.assign({}, myOldObject)' and then update your new record with the copy that you have obtained. Link here 'stackoverflow.com/questions/39506619/…'
yes it is global object. I changed as you said but same issue i am facing. I have updated my code above. still am i missing something?
|
1

Simply use array = array.slice(). That will create a new Array Object, trigger ChangeDetection etc

Comments

1

Try replacing your code with this

let item = this.createnode.find(item => item.name == data.name)
item.checked = data.checked;
this.createnode = [...this.createnode, item];

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.