compare to arrays of objects and update key in angular 6 typescript

Related searches
listOne: [
{ 
  id: 1,
  compId: 11,
  active: false, 
},
{ 
  id: 2,
  compId: 22,
  active: false, 
},
{ 
  id: 3,
  compId: 33,
  active: false, 
},
]

listTwo: [
{ 
  id: 1,
  compId: 11,
  active: true, 
},
{ 
  id: 2,
  compId: 33,
  active: false, 
},
]

I have two json, here how to compare with compId key and update the active key in listOne from listTwo if compId is same.

In AngularJs I have tried with below this related link for AngularJs

But how to integrate with Angular 6 with Typescript.

And expected output is listOne: [ { id: 1, compId: 11, active: true, }, { id: 2, compId: 22, active: false, }, { id: 3, compId: 33, active: false, }, ]

You can use map and filter like this

listOne = listOne.map(item => {
       let exist = listTwo.filter(c=>c.compId == item.compId)[0];
       if(exist != undefined){

           item.active = exist.active;
           return item;
       }else{
           return item;
       }
    });

let listOne= [
{ 
  id: 1,
  compId: 11,
  active: false, 
},
{ 
  id: 2,
  compId: 22,
  active: false, 
},
{ 
  id: 3,
  compId: 33,
  active: false, 
},
]

let listTwo= [
{ 
  id: 1,
  compId: 11,
  active: true, 
},
{ 
  id: 2,
  compId: 33,
  active: false, 
},
]

//let arr3 = [];

listOne = listOne.map(item => {
   let exist = listTwo.filter(c=>c.compId == item.compId)[0];
   if(exist != undefined){
       //item.id = exist.id;
       item.active = exist.active;
       return item;
   }else{
       return item;
   }
});

console.log(listOne);

How to compare by two objects typescript angular6, hasOwnProperty( p ) ) return false; // allows to compare x[ p ] and y[ p ] when set to Object.equals( x[ p ], y[ p ] ) ) return false; // Objects and Arrays must be tested const keys1 = []; const values1 = []; Object.keys(obj1). KeyValue pipe released in Angular 6.1 to loop through objects,Maps and arrays.Now by passing KeyValue pipe to *ngFor we can loop through objects key values & maps. Prior to this Angular 6.1 release we cannot iterate directly through objects key values & maps using *ngFor directive. To tackle this issue Angular keyvalue pipe has been introduced.

Try this,

// Iterate over list one
for(let item of this.listOne){
    // Check if the item exist in list two
    if(this.listTwo.find(i=>i.compId==item.compId)){
        // Update key
        item.active = this.listTwo.find(i=>i.compId==item.compId).active;
    }
}

Check if two arrays or objects are equal with JavaScript, To properly compare two arrays or objects, we need to check: For objects, we can use Object.keys() to get an array of the object's keys, and� The object contains key date property. We can sort the object data based on date ascending or descending. Need to supply comparator custom code which handles Date comparisons. First Date string is converted to an Inbuilt Date object. Date ascending This is an example sort array of object date by date ascending

How to check if array includes a value in JavaScript?, Here's a Code Recipe to check if a #JavaScript array contains a value. Taking that concept, we can also use it to compare object element in an array like this: the in operator to loop over an object to check if a specific property key exists. 1. Angular 8.0.3 2. TypeScript 3.4.3 3. Node.js 12.5.0 4. Angular CLI 8.0.6 FormArray setValue() setValue() sets the value of the FormArray. We need to pass an array that matches the structure of the control. Create a sample FormArray.

How to Compare 2 Objects in JavaScript, Objects are reference types so you can't use === or == to compare them. To check if 2 objects have the same key value, use JSON.stringify OR Lodash isEqual function. If you have small arrays it's okay but for my personal use cases with more than 1000 events/products in 6 Use Case of Spread with Array in JavaScript� Browse other questions tagged arrays json angularjs angular typescript or ask your own question. The Overflow Blog How we built it: our new Articles feature for Stack Overflow Teams

How to Check If an Array Includes an Object in JavaScript, An array of objects; var persons = [{name: "Harry"}, {name: "Alice"}, {name: "Peter" }];; // Find if the array contains an object by comparing the property value came here looking for a solution while maintaining object reference between methods. this worked perfectly. thank you – jgritten Dec 6 '19 at 18:22 This doesn't merge the arrays, it adds data from the second into the first.

Convert Object to Array Example. This section will see how we can convert Object to Array in Angular and Typescript with examples. Typescript Code. Let us assume that you have an object declared multiple properties. First Get the named keys using object.keys() method. This method retrieves keys from the given object and returns an array of keys.

Comments
  • use listOne.forEach...
  • Have you tried implementing it similar to the linked question? for loops and array prototype methods work just the same in typescript
  • What is expected output?
  • Possible duplicate of Compare to arrays of objects and update key in Angular