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
4 Answers
Reset to default 4Using 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
版权声明:本文标题:javascript - Remove duplicate values array ionic3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744246625a2597046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论