-3

I have an array of objects:

[
  {
   id: 1,
   sku: 'aaaa'
  },
  {
   id: 2,
   sku: 'bbbb'
  },
  {
   id: 3,
   sku: 'cccc'
  },
  {
   id: 4,
   sku: 'aaaa'
  }
]

How can I check that there are more than one element with the same sku property existing in the array?

For example: I have to elements with the same prop sku ('aaaa') - and in this case I need to do something in other case - nothing

Mikhail Kostiuchenko
  • 9,121
  • 4
  • 15
  • 28

4 Answers4

3

Use set to get an array holding unique skus and compare the length

var skus = items.map(v=>v.sku);
var uniqueSkus = new Set(skus);
console.log(skus.length === uniqueSkus.size)
lastr2d2
  • 3,604
  • 2
  • 22
  • 37
0

You can use example code like this:

var a = [
  {
   id: 1,
   sku: 'aaaa'
  },
  {
   id: 2,
   sku: 'bbbb'
  },
  {
   id: 3,
   sku: 'cccc'
  },
  {
   id: 4,
   sku: 'aaaa'
  }
];

var b = {};
a.forEach(el => {
  if (b[el.sku]) {
    b[el.sku].count++;
  } else {
    b[el.sku] = { id: el.id, count: 1 };
  }
});

console.log(b);
Mahdi Aslami Khavari
  • 1,755
  • 15
  • 23
  • In order to answer OP's question you'll need to iterate once again through `Object.values(b)` to find out whether there exists `count > 1`, so, it's, basically, half of the answer – Yevhen Horbunkov Jul 22 '20 at 08:13
0

You can check like below array function 'some' will help you

 var arr = [
  {
   id: 1,
   sku: 'aaaa'
  },
  {
   id: 2,
   sku: 'bbbb'
  },
  {
   id: 3,
   sku: 'cccc'
  },
  {
   id: 4,
   sku: 'aaaa'
  }
]

const val = 'aaaa'

const isAvailable = arr.some(function(arrVal) {
        return val === arrVal.sku;
});

//here isAvailable will return true or false
Anku Singh
  • 914
  • 6
  • 12
0

const a = [
  {
   id: 1,
   sku: "aaaa"
  },
  {
   id: 2,
   sku: "bbbb"
  },
  {
   id: 3,
   sku: "cccc"
  },
  {
   id: 4,
   sku: "aaaa"
  }
];

a.reduce((acc, obj)=>{

    // looping through each sku & adding them in acc
    acc.push(obj.sku);
    
    // check if current sku already exits in acc.
    const duplicate = acc.filter(x=>x === obj.sku);
    
    if(duplicate.length>1){
          // do something or nothing with current obj as similar SKUs are found
          // i'm adding a new property called x=10 if same SKUs are found.
          obj.x = 10;
    }else{
          // do something or nothing with current obj as similar SKUs are found
          // i'm adding a new property called y=10 if not same SKUs.
          obj.y=10;
 }
 
 return acc;

}, []);

console.log(a);
micronyks
  • 54,797
  • 15
  • 112
  • 146
  • Your code does nested lookups with `.filter()` inside `.reduce()` (O(n²)-time algorithm), which will perform unnecessarily slow if source array happens to be large enough. Furthermore, it doesn't make much sense to use `.reduce()` as long as you don't really use `acc`, so simple `.forEach()` would work pretty much fine. – Yevhen Horbunkov Jul 22 '20 at 08:29
  • OP doesn't ask for returning true or false either. – micronyks Jul 22 '20 at 08:33
  • foreach can also be used but reduce would be a little performant. – micronyks Jul 22 '20 at 08:35
  • *'...reduce woudl be ..performant'* if performance is your concern, you would gain much more with turning your algorithm to O(n)-time rather than doing microoptimizations with `.reduce()` vs `.forEach()` – Yevhen Horbunkov Jul 22 '20 at 08:41
  • *'...OP doesn't ask'*, OP's request was rather vague, but It seems to me executing some code block conditionally upon `true` or `false` wouldn't take them much effort – Yevhen Horbunkov Jul 22 '20 at 08:43
  • That way you can also say that OP's question is kinda incomplete. he doesn't talk about what exactly he wants. But of course, he doesn't want true or false answer as per his question. – micronyks Jul 22 '20 at 08:45
  • Probably, I don't have that much of a mental connection to OP to know what they wanted, however `true`/`false` seems to me whole lot better suited answer to the question *'How can I check that there are more than one element'* than console logging input array after suboptimal looping through it using inappropriate array method. `for(..`-loop would perform even better, by the way, but would appear much easier to comprehend. – Yevhen Horbunkov Jul 22 '20 at 08:52