admin管理员组

文章数量:1401673

I would like to remove duplicate values in the storage array in Ionic3

this.storage.get('thestations').then((val) => { 

           for(let i =0;i<val.length;i++){
            if(this.newarray.indexOf(this.newarray) == -1) {
            this.newarray.push(val[i]);
            } 
                console.log(newarray);

      });

But it still return duplicates values

I would like to remove duplicate values in the storage array in Ionic3

this.storage.get('thestations').then((val) => { 

           for(let i =0;i<val.length;i++){
            if(this.newarray.indexOf(this.newarray) == -1) {
            this.newarray.push(val[i]);
            } 
                console.log(newarray);

      });

But it still return duplicates values

Share Improve this question edited Jan 7, 2019 at 6:35 Kiwi Rupela 2,3485 gold badges28 silver badges45 bronze badges asked Oct 26, 2017 at 10:13 de albuquerque frankde albuquerque frank 1973 silver badges16 bronze badges 3
  • What is format of val object? – Paresh Gami Commented Oct 26, 2017 at 11:35
  • Please update the question with your array. – Hassan Imam Commented Oct 26, 2017 at 12:00
  • You are testing whether one of the items in newarray matches the entire newarray, which I doubt is your goal. – Guinn Commented Oct 26, 2017 at 12:09
Add a ment  | 

4 Answers 4

Reset to default 4

Using Set

var val = ["Banana", "Orange", "Apple", "Mango", "Mango"];
var newarray =  Array.from(new Set(val));

check here using Reduce

var val = ["Banana", "Orange", "Apple", "Mango", "Mango"];
var newarray = val.reduce(function(res, ele) {
    if(res.indexOf(ele)==-1)
       res.push(ele);
return res;
}, [])

One-liner: [...new Set(["Banana", "Orange", "Apple", "Mango", "Mango"])]

this.storage.get('thestations').then(val => {  
  console.log('with duplicates',val);   
  console.log('without duplicates',[...new Set(val)]);
})

I don't know, you solved your problem or not, but I have one of the most efficient answer for you.

Javascript provides lodash library for us. For full documentation, please visit Lodash Documentation

Lodash provides us with thousands of built-in functions like filter, unique, isEqual and lots more.

Step 1: You just need to install the npm package of lodash by following mand.

npm install lodash --save

Step 2: Import the dependency in your ponent file.

import * as lodash from 'lodash';

Step 3: Now consider you have array named as abcArr = [5,10, 8, 11, 10, 8]; And you want to ignore all the repeated numbers.

Use the following code to get the expected output.

let abcArr = [5,10, 8, 11, 10, 8];
let uniqueArr = []; 
uniqueArr = lodash.uniqWith(abcArr, lodash.isEqual);

Hence you will get your expected solution like above. Hope so, it will help you.

You have to use function like below

var val = ["Banana", "Orange", "Apple", "Mango", "Mango"];
var newarray = [];

for(let i=0; i < val.length; i++)
{
    if(newarray.indexOf(val[i]) == -1)
        newarray.push(val[i]);
}
console.log(newarray);

</script>

You have to change val variable as per your requirement.

本文标签: javascriptRemove duplicate values array ionic3Stack Overflow