Find duplicates of some keys in an array of JavaScript objects and set the corresponding key to true

I have an array of JS objects:

  var a = [{a:"a1",b:"b",c:"c",prop:false},
           {a:"a1",b:"b",c:"c",prop:false},
           {a:"a",b:"b",c:"c",prop:false},
           {a:"a2",b:"b",c:"c",prop:false},
           {a:"a3",b:"b",c:"c",prop:false},
           {a:"a1",b:"b",c:"c",prop:false}];

I should find duplicates of the last element, but NOT looking at the key prop: when I find them, the prop boolean associated with it should be set to true.

I did the simplest thing in the world and it works great, but I wandered if there is something that JS can do (without third-party libraries), better either in "elegance", news, or performance:

var toTest = a[a.length-1];
for(var i=0;i<a.length;i++){
      if(toTest.a==a[i].a && toTest.b==a[i].b && toTest.c==a[i].c){
         a[i].prop = true;
         a[a.length-1].prop = true;
      }
    }

Any advice is welcome.

+4
source share
4 answers

Array.forEach Array.every. , .

a[a.length-1].prop = true , , .

var a = [{a:"a1",b:"b",c:"c",prop:false},
         {a:"a1",b:"b",c:"c",prop:false},
         {a:"a",b:"b",c:"c",prop:false},
         {a:"a2",b:"b",c:"c",prop:false},
         {a:"a3",b:"b",c:"c",prop:false},
         {a:"a1",b:"b",c:"c",prop:false}];
var toTest = a[a.length -1];
var keysToTest = ['a', 'b', 'c'];

a.forEach((o)=>{
  var valid = keysToTest.every((k) => o[k] === toTest[k]);
  if(valid){
    o.prop = true;
  }
});

console.log(a)
Hide result
+1

Array.filter() , a, b c.

: . , Array.filter() - .

+1

, , . , , , , , i < a.length - 1 ( prop ).

, :

  • a[a.length-1].prop = true; toTest.prop = true;
  • a[i] ,
  • a, b c, ( JavaScript , ).
  • ES2015 + a, b c

, :

// Caching the last item
var last = a[a.length - 1];
// Caching the values to check
var checka = last.a, checkb = last.b, checkc = last.c;
// Stop at i < a.length - 1, caching that to l
for (var i = 0, l = a.length - 1; i < l; ++i) {
    // Cache a[i]
    var entry = a[i];
    if (entry.a == checka && entry.b == checkb && entry.c == checkc) {
        entry.prop = last.prop = true;
    }
}

Live Copy:

var a = [{a:"a1",b:"b",c:"c",prop:false},
           {a:"a1",b:"b",c:"c",prop:false},
           {a:"a",b:"b",c:"c",prop:false},
           {a:"a2",b:"b",c:"c",prop:false},
           {a:"a3",b:"b",c:"c",prop:false},
           {a:"a1",b:"b",c:"c",prop:false}];

// Caching the last item
var last = a[a.length - 1];
// Caching the values to check
var checka = last.a, checkb = last.b, checkc = last.c;
// Stop at i < a.length - 1, caching that to l
for (var i = 0, l = a.length - 1; i < l; ++i) {
    // Cache a[i]
    var entry = a[i];
    if (entry.a == checka && entry.b == checkb && entry.c == checkc) {
        entry.prop = last.prop = true;
    }
}
console.log(a);
.as-console-wrapper {
  max-height: 100% !important;
}
Hide result

ES2015 +, :

const last = a[a.length - 1];
const {a: checka, b: checkb, c: checkc} = last;
for (let i = 0, l = a.length - 1; i < l; ++i) {
    const {a: thisa, b: thisb, c: thisc} = a[i];
    if (thisa == checka && thisb == checkb && thisc == checkc) {
        a[i].prop = last.prop = true;
    }
}

Live Copy:

const a = [{a:"a1",b:"b",c:"c",prop:false},
           {a:"a1",b:"b",c:"c",prop:false},
           {a:"a",b:"b",c:"c",prop:false},
           {a:"a2",b:"b",c:"c",prop:false},
           {a:"a3",b:"b",c:"c",prop:false},
           {a:"a1",b:"b",c:"c",prop:false}];

const last = a[a.length - 1];
const {a: checka, b: checkb, c: checkc} = last;
for (let i = 0, l = a.length - 1; i < l; ++i) {
    const {a: thisa, b: thisb, c: thisc} = a[i];
    if (thisa == checka && thisb == checkb && thisc == checkc) {
        a[i].prop = last.prop = true;
    }
}
console.log(a);
.as-console-wrapper {
  max-height: 100% !important;
}
Hide result

.

+1

forEach Loop

var a = [{a:"a1",b:"b",c:"c",prop:false}, {a:"a1",b:"b",c:"c",prop:false}, {a:"a",b:"b",c:"c",prop:false}, {a:"a2",b:"b",c:"c",prop:false},{a:"a1",b:"b",c:"c",prop:false},{a:"a1",b:"b",c:"c",prop:false}];     
var toTest = a[a.length-1];              
a.forEach(function(item){
  item.prop = (item.a == toTest.a && item.b == toTest.b && item.c == toTest.c) ? true : false;
});
console.log(a);
Hide result
+1

Source: https://habr.com/ru/post/1684665/


All Articles