Removing duplicates in array in javaScript using hasOwnProperty [duplicate] - javascript

This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 5 months ago.
I have an array with duplicate elements. While trying to remove duplicate elements using hasOwnProperty getting one duplicate element in array rest of duplicate element removed successfully. expexted output = [1, 3, 2, 4, 5, 6, 7] but getting something [1, 3, 2, 3, 4, 5, 6, 7]. I can use different function and remove duplicates but I'm not understanding why element 3 is coming twice.
var array = [1,3,2,1,3,4,5,6,7,3,5,6,4,3]
let output = []
function removeDuplicates(array){
for(let item of array){
if(!output.hasOwnProperty(item))
output.push(item)
}
return output
}
console.log(removeDuplicates(array));

Instead of using hasOwnProperty, you can use includes.
var array = [1,3,2,1,3,4,5,6,7,3,5,6,4,3]
let output = []
function removeDuplicates(array){
for(let item of array){
if(!output.includes(item))
output.push(item)
}
return output
}
console.log(removeDuplicates(array));

hasOwnProperty checks whether an object contains a given key. The correct way to check if an array contains an element is to use includes:
if(!output.includes(item))
output.push(item)

Related

How is JavaScript splice affecting the array even before stating that I want to splice it? [duplicate]

This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Wrong value in console.log [duplicate]
(4 answers)
Closed 25 days ago.
Screenshot of the Output
This is a simplified version of the problem =>
const z = [
[1, 2, 3, 4, 5]
];
console.log(z);
z[0].splice(1, 1);
console.log(z);
Both Console logs output :
Array [ (5) […] ]
0: Array(4) [ 1, 3, 4, … ]
​length: 1
How is splice affecting the first console log?
​
I was making an algorithm then I noticed that even before calling the splicing function my original array which had thousands of items was getting affected meaning that the "History" or "Log" array that I was trying to form always had the same spliced version of the array that i later on tried splicing.
JavaScript's splice() method modifies the original array, meaning that it directly alters the array on which it is called. This is different from some other methods, such as slice(), which create and returns a new array without modifying the original.
const z = [
[1, 2, 3, 4, 5]
];
console.log(z);
//line below will affect the original array because array is
// refference in js
// z[0].splice(1, 1);
//instaed make a new deep copy of your variable z
const z2 = JSON.parse(JSON.stringify(z));
//now make the required changes to z2
z2[0].splice(1, 1);
console.log(z2);

comparing elements in an array in JS [duplicate]

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed last month.
How can I compare elements in an array like if I for example have arr=[1, 2, 3, 4, 4] and I wanna find out if there are any duplicates and remove them.Are there any fuctions that I can use for this?
you can use a set data structure inbuilt into JS
let arr=[1, 2, 3, 4, 4] ;
let output = [...new Set(arr)]
You need to convert the array to a set, A set has unique elements only as opposed to arrays that can have duplicates. To convert to a set
let arr=[1, 2, 3, 4, 4]
let set = new Set(arr);

How to create array list from for loop in react [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 12 months ago.
I am working on a project in VS built with react (I'm new to react) and I have a loop function that returns json data and I am trying to create a list/array - [1, 2, 3, 4] using the IDs from the loop -
for (const data of data.dataset) {
ID = data.id;
}
How can I achieve this?
let myArray = [];
for (let data of data.dataset) {
myArray.push(data.id)
}
console.log(myArray); // [1, 2, 3, 4]
You can use this method to create a new array with ID's.

How to Clone an Array With a Removed Element? [duplicate]

This question already has answers here:
How to get subarray from array?
(5 answers)
Closed 2 years ago.
I've searched up this question, and everywhere people seem to recommend to use array.splice(). However, splice is inplace, and, for example, in my javascript console editor.
Everywhere I seem to search, people say that splice does NOT mutate the original array, but that is clearly not the case. Now, I'm sure I will find another way to do what I want, but what is the proper way to make a copy of a piece of an array without affecting the original array?
You can use slice(), see below:
let x = [1, 2, 3, 4, 5]
console.log(x);
let sliced = x.slice(0, 2);
console.log(x);
console.log(sliced);
The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included) where begin and end represent the index of items in that array. The original array will not be modified.
Make a copy of the array using the spread operator and then you can use splice or whatever.
let arr = [1, 2, 3, 4, 5];
let newArr = [...arr];
console.log(newArr);
// newArr.splice(......)

JS insert into array at specific index [duplicate]

This question already has answers here:
How to insert an item into an array at a specific index (JavaScript)
(28 answers)
Closed 7 years ago.
I would like to insert a string into an array at a specific index. How can I do that?
I tried to use push()
Well, thats pretty easy. Assuming you have an array with 5 objects inside and you want to insert a string at index 2 you can simply use javascripts array splice method:
var array = ['foo', 'bar', 1, 2, 3],
insertAtIndex = 2,
stringToBeInserted = 'someString';
// insert string 'someString' into the array at index 2
array.splice( insertAtIndex, 0, stringToBeInserted );
Your result will be now:
['foo', 'bar', 'someString', 1, 2, 3]
FYI: The push() method you used just adds new items to the end of an array (and returns the new length)

Categories

Resources