-2

I have a form in HTML. In this form, I can build catalog preferences. When I'm done with that I can click on save button and build a next one. The result is saving in an array.

When I have more than one saved catalog(sku=stock keeping unit) in my array it's possible that there are some duplicates. How can I remove the duplicates and count them (The quantity should stay even if there is a duplicate)?

Jsfiddle

Array(Array(
      0:5016s18gercol, //sku(stock keeping unit)
      1: 100,          //quantity
      2: 5017ext10002, //extra sku for extra costs(cataloge in color)
      3: 1
      ),
     Array(
      0:5016s43gerbw, //sku
      1: 100,         //quantity
      2: 5017ext10001,//extra sku for extra costs(catalog own cover)
      3: 1            //quanitity extra costs
     ),
     Array(
      0: "5016s43gercol" //sku 
      1: "400"           //quantity
      2:"5017ext10001"   //extra sku (own cover)
      3:"1"              //quantity sku
      4:"5017ext10002"   //extra sku (in color)
      5:"1"              //quantity sku
     )
 )

This is what i get. It should look like this:

array(5016s18gercol,
500,
5017ext10002,
2,
5017ext10001,
2)

Every sku is unique so i cant have two same sku's. I have to count them if there are more than one.

4
  • 3
    You're aware that besides arrays, JavaScript also has objects, right? Commented Jul 27, 2017 at 9:04
  • okey so how can i do this with objects ? Commented Jul 27, 2017 at 9:08
  • possible duplicates of:- stackoverflow.com/a/21489705/4248328 Commented Jul 27, 2017 at 9:10
  • its helpful but not a possible duplicate. i have to eliminate and count duplicates. Commented Jul 27, 2017 at 9:20

1 Answer 1

0

Use array.prototype.map and array.prototype.some:

var values = [
    { name: 'someName1' },
    { name: 'someName2' },
    { name: 'someName4' },
    { name: 'someName2' }
];

var valueArr = values.map(function(item){ return item.name });
var isDuplicate = valueArr.some(function(item, idx){ 
    return valueArr.indexOf(item) != idx 
});
console.log(isDuplicate);
Sign up to request clarification or add additional context in comments.

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.