0

I am unable to remove the duplicate value from array using JavaScript.

var arr = [{
  'image': "jv2bcutaxrms4i_img.png",
  'gallery_image': true
}, {
  'image': "abs.png",
  'gallery_image': true
}, {
  'image': "acd.png",
  'gallery_image': false
}, {
  'image': "jv2bcutaxrms4i_img.png",
  'gallery_image': true
}, {
  'image': "abs.png",
  'gallery_image': true
}, {
  'image': "acd.png",
  'gallery_image': false
}]

var outputList = [];

for (var i = 0; i < arr.length; i++) {
  if (outputList.indexOf(arr[i].image) == -1) {
    var data = {
      image: arr[i].image,
      gallery_image: arr[i].gallery_image
    };
    outputList.push(data);
  }
}

console.log(outputList)

Here I can not delete the data properly.

halfer
  • 19,824
  • 17
  • 99
  • 186
satya
  • 3,508
  • 11
  • 50
  • 130
  • what do you do with the value of `gallery_image`? which one should be kept? – Nina Scholz Sep 28 '16 at 12:21
  • 1
    Don't you think image name should be in string format? – suyesh Sep 28 '16 at 12:21
  • @suyesh: Yes,that is in string format. – satya Sep 28 '16 at 12:22
  • @NinaScholz : I have this ` {'image':jv2bcutaxrms4i_img.png,'gallery_image':true}` multiple times i just remove . – satya Sep 28 '16 at 12:22
  • Possible duplicate of [Remove duplicates from an array of objects in javascript](http://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-objects-in-javascript) – JJJ Sep 28 '16 at 12:24

3 Answers3

3

Instead of indexOf() method you should use some(). From MDN:

The some() method tests whether some element in the array passes the test implemented by the provided function.

if (!outputList.some(x => x.image === arr[i].image))

Working demo

var arr=[
{'image':'jv2bcutaxrms4i_img.png','gallery_image':true},
{'image':'abs.png','gallery_image':true},
{'image':'acd.png','gallery_image':false},
{'image':'jv2bcutaxrms4i_img.png','gallery_image':true},
{'image':'abs.png','gallery_image':true},
{'image':'acd.png','gallery_image':false}
]

var outputList=[];

for(var i = 0; i < arr.length; i++){
   if (!outputList.some(x => x.image === arr[i].image)){
   var data={ image: arr[i].image,gallery_image:arr[i].gallery_image };
   outputList.push(data);
   }
}

console.log(outputList)
Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
0

You could filter it with a hash table.

var arr = [{ 'image': 'jv2bcutaxrms4i_img.png', 'gallery_image': true }, { 'image': 'abs.png', 'gallery_image': true }, { 'image': 'acd.png', 'gallery_image': false }, { 'image': 'jv2bcutaxrms4i_img.png', 'gallery_image': true }, { 'image': 'abs.png', 'gallery_image': true }, { 'image': 'acd.png', 'gallery_image': false }];

arr = arr.filter(function (a) {
    if (!this[a.image]) {
        this[a.image] = true;
        return true;
    }
}, Object.create(null));

console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
-1

You're going to have to compare elements of the object in the arrays:

var arr = [
    { 'image': 'jv2bcutaxrms4i_img.png', 'gallery_image': true },
    { 'image': 'abs.png', 'gallery_image': true },
    { 'image': 'acd.png', 'gallery_image': false },
    { 'image': 'jv2bcutaxrms4i_img.png', 'gallery_image': true },
    { 'image': 'abs.png', 'gallery_image': true },
    { 'image': 'acd.png', 'gallery_image': false }
]

var unique = [];

arr.forEach(function(obj){
    var found = false;
    unique.forEach(function(uniqueObj){
        if(uniqueObj.image == obj.image) {
            found = true;
        }
    })
    if(!found){
        unique.push(obj);
    }
})

I see what you're trying to do, but indexOf won't search the array for an object property.

Jeremy Jackson
  • 2,247
  • 15
  • 24