This question already has answers here:
Array.includes() to find object in array [duplicate]
(8 answers)
Check if an array contains any element of another array in JavaScript
(32 answers)
Closed 2 years ago.
var coords = [
[0,0],[5,0],[0,5],[5,5],[8,5],[8,0],[40,5],[5,54],[6,7],[40,0],[8,54]
]
console.log(coords.includes([0,0]))
The result here is false, and I am unsure why. Please help
Array.prototype.includes compares the values using the strictly equal operator. Arrays are stored by reference and not by value. [] === [] will never be true, unless you are comparing the same array stored in your array.
A way to solve this issue is using Array.prototype.some
var coords = [
[0,0],[5,0],[0,5],[5,5],[8,5],[8,0],[40,5],[5,54],[6,7],[40,0],[8,54]
]
console.log(coords.some(coordinate => {
const [x, y] = coordinate
// This is the value you are comparing
const myCoords = [0, 0]
return x === myCoords[0] && y === myCoords[1]
}))
Related
This question already has answers here:
How to check if an array is a subset of another array in JavaScript?
(9 answers)
Function with forEach returns undefined even with return statement
(5 answers)
Closed 2 years ago.
I'm struggling a bit with higher-order functions. I want to compare two arrays to see if all the values of one array are part of another array. In that case I want to return true, otherwise false.
My code:
let arrayA = [4,5,6,7]
let arrayB = [4,5,6,7,8];
let result = arrayA.forEach(number => {
if (!arrayB.includes(number)){
return false
}
else {
return true
}
})
console.log(result) //=> undefined
Thanks for reading or even helping a beginner out!
.forEach is not suitable here (it does not return). Use .some and .indexOf as follows:
let arrayA = [4,5,6,7]
let arrayB = [4,5,6,7,8];
let result = !arrayA.some(val => arrayB.indexOf(val) === -1);
console.log(result)
This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 3 years ago.
For eg.
[
{
x:1,
y:2
},
{
x:10,
y:20
},
]
How can I check if x exists in both of the objects inside the array?
DESIRED OUTPUT:
if x doesn't exist in even one object inside the array ---> false
else ---->true
I have tried using the array.prototype.find() method but not able to find the right logic to get the desired output.
You could check the objects with Object.hasOwnProperty and the wanted property and take Array#every for checking all elements of the array.
var array = [{ x: 1, y: 2 }, { x: 10, y: 20 }],
result = array.every(o => o.hasOwnProperty('x'));
console.log(result);
You can use the operator in for checking if a property exists in the object, this along with the function every.
let arr = [{x:1,y:2},{x:10,y:20}];
console.log(arr.every((o) => "x" in o))
console.log(arr.every((o) => "z" in o))
This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 3 years ago.
There is a string variable that has a path like /app/something/xx/4/profile
besides there is an array of string like **
const arr=[
{name:'xx',etc...},
{name:'yy',etc...},
{name:'zz',etc...}
]
I want to find the first index of array that the string variable has the name in simplest way.
Use Array.findIndex which :
returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
const str = "/app/something/xx/4/profile";
const arr = [{ name: "xx" }, { name: "yy" }, { name: "zz" }];
const index = arr.findIndex(e => str.includes(e.name));
console.log({ index });
This question already has answers here:
Why is [] !== [] in JavaScript? [duplicate]
(3 answers)
How to check if two arrays are equal with JavaScript? [duplicate]
(16 answers)
Closed 4 years ago.
This appears as a very basic question, but couldn't find any explanations on SO.
Consider this:
var arr = [1, 2, 3];
var str = "123";
function compare(){
return arr.join('').split('') === str.split('')
}
console.log(compare());
console.log(arr.join('').split(''))
console.log(str.split(''))
Cant understand why console logs false...?
Because although the two arrays you're comparing are equivalent, they are not the same array. == (and ===) with objects checks to see if they're the same object.
What you have, in effect, is:
console.log(["1", "2", "3"] == ["1", "2", "3"]);
You compare two objects' references, not their content. Because you have 2 different objects, their references are not equal.
=== with reference types will return true if 2 variables refer to the same object. In the below example both a and b refer to the same object (same part in the memory), so they are equal.
const a = {};
const b = a; // assign `a`'s value, which is a reference to the `b`
console.log(a === b);
They are not same array & they are separate object reference. If you want to compare these two , then stringify them
var arr = [1, 2, 3];
var str = "123";
function compare() {
return (arr.join('').split('')).toString() === str.split('').toString()
}
console.log(compare());
console.log(arr.join('').split(''))
console.log(str.split(''))
This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Closed 8 years ago.
I don't want to compare the two arrays as a whole, I specifically want to check if the 2nd array has any values that match a value in the first one. I then want to return the value that matches in both.
If I'm not mistaken, comparing two arrays as a whole would be done like this:
array1 = [1,2,3];
array2 = [1,3,4];
console.log(JSON.encode(array1)==JSON.encode(array2));
So in this case, I would want to check if array2 has any matching values to array one, not if they both are equivalent. Any help is appreciated!
var array1 = [1, 2, 3],
array2 = [1, 3, 4];
var AnyItemsOfArray1InArray2 = array1.some(function(item) {
return array2.indexOf(item) > -1;
});
console.log(AnyItemsOfArray1InArray2);
var ItemsOfArray1InArray2 = array1.filter(function(item) {
return array2.indexOf(item) > -1;
});
console.log(ItemsOfArray1InArray2);