comparing elements in an array in JS [duplicate] - javascript

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);

Related

Removing duplicates in array in javaScript using hasOwnProperty [duplicate]

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)

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)

Selecting a value in an array in an array; Javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
I have an array, that happens to be filled with arrays: [[1, "thing"], [2, "other thing"], [3, "still other thing"]] I want to be able to select a value inside one of the arrays without having to make the array a temporary variable and then pick the item. Can this be done?
You can index into a nested array with the same '[]' syntax.
var arr = [[1, "thing"], [2, "other thing"], [3, "still other thing"]];
console.log(arr[0]); // [1, "thing"]
console.log(arr[0][0]); // 1
For reference, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
In that link search for two-dimensional

Categories

Resources