How to remove duplicated objects from an Array in javascript [duplicate] - javascript

This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 6 years ago.
I am using above code and removing the items based on id
I am getting id is not defined while comparing i+1 item when i reaches to maximum array length.
Appreciate your help....
var arr = [{"id":"0-block-0","left":206.5,"top":0},{"id":"0-block-1","left":446.5,"top":0},{"id":"0-block-2","left":474.5,"top":16},{"id":"0-block-2","left":686.5,"top":0}];
Expecting outout = [{"id":"0-block-0","left":206.5,"top":0},{"id":"0-block-1","left":446.5,"top":0},{"id":"0-block-2","left":686.5,"top":0}]
Array.prototype.unique = function(){
var passNum = this.length;
var _self = this;
// to repeat loops n*n times
while(passNum>0){
for(var i = 0;i<this.length;i++) {
if(this[i].id==this[i+1].id){
var _indx = this.indexOf(this[i]);
this.splice(_indx,1);
}
}
passNum = passNum-1;
}
return this;
};

You can do this with ES2015 Sets.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
EDIT: If you can't use ES2015, there is a polyfill available.

Related

JavaScript Array undefined element [duplicate]

This question already has answers here:
How can I add new array elements at the beginning of an array in JavaScript?
(12 answers)
Closed 6 years ago.
I´ve got a function which should add an element at the start of an array.
But I always get an undefined element at the end of my array. I hope someone can help me :)
function putToFirst(e){
var array = [];
array.push(e);
this.arrayList = array.concat(this.arrayList);
}
EDIT:
class List {
constructor () {
super()
this.arrayList = [];
}
putToFirst(e) {
this.ArrayList.unshift(e);
}
}
thats the class. I create a new object from the class list and call the function putToFirst on this object. But I always get an Array with 'undefinded' in the end
this.arraylist is wrong this represent a current object you are not currently using any class in above code you just need to change
function putToFirst(e){
var array = [];
var arrayList=[];
array.push(e);
arrayList = array.concat(arrayList);
console.log(arrayList);
}

How to get unique array with only filter in Javascript [duplicate]

This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 6 years ago.
I have an array:
var a = [2,3,4,5,5,4]
I want to get unique array out of given array like
b = [2,3,4,5]
I have tried
a.filter(function(d){return b.indexOf(d)>-1})
and I don't want to use for loop.
You can simply do it in JavaScript, with the help of the second - index - parameter of the filter method:
var a = [2,3,4,5,5,4];
a.filter(function(value, index){ return a.indexOf(value) == index });
This is not an Angular related problem. It can be resolved in a couple of ways depending which libraries you are using.
If you are using jquery:
var uniqeElements = $.unique([2,3,4,5,5,4]);
The output would be:
[2,3,4,5]
If you are using underscore:
var uniqeElements = _.uniq([2,3,4,5,5,4]);
Same output.
And pure JS code:
var unique = [2,3,4,5,5,4].filter(function(elem, index, self) {
return index == self.indexOf(elem);
})
var b = a.filter( function( item, index, inputArray ) {
return inputArray.indexOf(item) == index;
});

Weird behaviour in Array.fill [duplicate]

This question already has answers here:
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 4 years ago.
When i use Array.fill to fill a multidimensional array, i get a weird behaviour when pushing to one of the arrays:
var arr = Array(2).fill([]);
arr[0].push(5);
console.log(arr);
//=> prints [[5], [5]]
fill is essentially doing this:
var content = [];
for (var i = 0; i < 2; i += 1) {
arr[i] = content;
}
So, your array will have a reference to the array you've passed to fill in each property.
It sounds weird, but what your code actually does is create an array ([]) and put a reference for that array in each of the items of the Array(2). So whenever you change that reference - every array that is referenced to that Array is changed.
It's exactly the same as:
var a = [];
var arr = Array(2).fill(a);
a.push(5);
console.log(arr[0][0], arr[1][0]);
a[0] = 2;
console.log(arr[0][0], arr[1][0]);
You can see that the values inside the arr are affected by the change to the a array.

Dynamically create js Array filled with certain value [duplicate]

This question already has answers here:
Create an array with same element repeated multiple times
(25 answers)
Closed 7 years ago.
I'm wondering if there is a way to create javascript/jquery array in one-liner to receive something like:
my_array = ['-', '-', ,'-' ,'-']
Idea is that array should be created with dynamic length and all values filled with given value.
Thanks in advance!
Try:
var total = 4;
var my_array = new Array(total + 1).join("-").split("");
document.write(JSON.stringify(my_array))
.fill Support The native function will be added in (ECMAScript 6), but for now is not available.
if(!Array.prototype.fill){
Array.prototype.fill = function(val){
for (var i = 0; i < this.length; i++){
this[i] = val
}
return this
}
}
var my_array = new Array(4).fill("-"); //returns ["-","-","-","-"]
Try to use:
Array.apply(null, new Array(63)).map(String.prototype.valueOf,"-")

how to add repeating strings value in json array [duplicate]

This question already has answers here:
Adding a new array element to a JSON object
(6 answers)
Closed 7 years ago.
I have JSON as
var newJSON = [{"key":"India","value":"72"},{"key":"India","value":"27"},{"key":"Pakistan","value":"90"},{"key":"Zimbamwe","value":"88"},{"key":"India","value":"100"},{"key":"Pakistan","value":"172"}]
I want desired result as
[{"key":"India","value":"199"},{"key":"Pakistan","value":"262"},{"key":"Zimbamwe","value":"88"}]
Please help me with this
are you sure you want them as {"india","20"}??
or is it {"india":"20"} //this is recommended
then you can do
json["india"]
or json[0]
Update:
var yourObj={key:'india', value:72};
var newObj = {};
newObj[yourObj.key] = yourObj.value;
newObj["Austria"]=100; // adds as a new list object
You could use
var jsondata =[];
var firstvalue = {country:"India",number:"200"};
jsondata.push(firstvalue);
var secondvalue = {country:"Australia",number: "210"};
jsondata.push(secondvalue);
for(var i =0; i< jsondata.length;i++){
console.log(jsondata[i].country);
}

Categories

Resources