Why is my array remain unchanged after sort? [duplicate] - javascript

This question already has answers here:
How to sort 2 dimensional array by column value?
(14 answers)
Sort array of objects by string property value
(57 answers)
Closed last year.
My values in a nested array like in the code and I want to sort it by the item name, but the array remain unchanged after the sort.
let arr1 = [
[88, "Bowling Ball"],
[2, "Dirty Sock"],
[3, "Hair Pin"],
[5, "Microphone"],
[3, "Half-Eaten Apple"],
[7, "Toothpaste"] ]
arr1.sort( (a, b) => a[1] - b[1]);
console.log(arr1);

Related

Check whether array contains array [duplicate]

This question already has answers here:
Check if an array includes an array in javascript
(3 answers)
Closed 10 months ago.
Welcome,
I need to find whether array contains array
I tried two ways(indexOf and includes),but both returns a negative result.
let myarray = [
[4, 2],
[2, 2],
[2, 2],
[2, 4],
[2, 2],
];
console.log(myarray.includes([4, 2]));//Returns false
console.log(myarray.indexOf([4, 2]));//Returns -1
I then thought of having a foreach loop,and checking the array(using ==),
But for that even console.log([4, 2]==[4, 2]);//Returns false
Please Help-I know this is simple for you,
Use some and Array.isArray. some will return true if any one condition in the callback function is true and use Array.isArray to check if the element is an array
let myarray = [
[4, 2],
[2, 2],
[2, 2],
[2, 4],
[2, 2],
];
const hasNestedArray = myarray.some(elem => Array.isArray(elem));
console.log(hasNestedArray)

Javascript copying an array of arrays [duplicate]

This question already has answers here:
Create copy of multi-dimensional array, not reference - JavaScript
(4 answers)
Closed 8 months ago.
I want to copy an array of arrays at a different allocation.
I know that I can copy an array in the following way:
a = [[1, 2], [3, 4]]
b = a.slice() // this makes sure that a and b are allocated differently in memory
Now if I change something inside b, then of course,
b[0] = 'abc'
console.log(a, b) // expect a = [[1,2], [3,4]] and b = ['abc', [3,4]]
But when I do the below, a gets changed as well...!
b[0][0] = 'abc'
console.log(a, b) // now it gives a = [['abc', 2], [3, 4]] and b = [['abc', 2], [3, 4]]
Why is this happening, and how can I avoid mutating a?
Thanks so much!
If you know you are only copying 2D arrays you could use a function like the following and avoid using JSON:
function copy2D(array){
result = []
array.forEach((subArray) => {
result.push(subArray.slice())
})
return result
}
One way would be by using map combined with the spread operator. This would be the easiest approach if you can assume that you have a 2D array only
const a = [[1, 2], [3, 4]]
const b= a.map(item => ([...item]))
b[0][0]= "abc"
console.log('a:', a, 'b: ', b)

how I can solve aperture function in javascript? [duplicate]

This question already has answers here:
How to create windowed slice of array in JavaScript?
(3 answers)
Closed 3 years ago.
I want solve a function called aperture which accept a number and an array that should return new array that should be composed of subarrays the size of the number with consecutive elements, for example aperture:
(3, [1, 2, 3, 4, 5]); // [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
Create an empty array whose length is equal to the given number and use reduce() on it.
const aperture = (num,arr) => [...Array(num)].reduce((ac,_,i) => {
ac.push(arr.slice(i,num+i));
return ac;
},[])
console.log(aperture(3,[1,2,3,4,5]))

String to array of array or elements [duplicate]

This question already has answers here:
Converting a string to JSON object
(10 answers)
Closed 3 years ago.
Is there any in build function in javascript that let's me convert string "[[16, [8, 2], 4], 2, 80]" to an array of array or int [[16, [8, 2], 4], 2, 80]
You can use:
JSON.parse("[[16, [8, 2], 4], 2, 80]")

JS: Make new array form duplicate values [duplicate]

This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Compute intersection of two arrays in JavaScript [duplicate]
(4 answers)
Finding matches between multiple JavaScript Arrays
(13 answers)
Closed 5 years ago.
Let's take two arrays for example:
a = [1, 2, 3, 4, 5]
b = [4, 5, 6, 7, 8]
Now there is duplicates as we see 4 and 5. How to make new array from them instead of getting rid of them. What is the easiest solution?
So new array should be like:
newArray = [4, 5]
Thank you guys in advance!
You can do it using Array.filter() and Array.includes()
let a = [1, 2, 3, 4, 5];
let b = [4, 5, 6, 7, 8];
let arr = a.filter(function(x){
return b.includes(x);
})
console.log(arr);

Categories

Resources