This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 4 months ago.
In Javascript, I have this
var a = ["1","2","3"]
var b = ["3","4","5"]
assuming "b" reads "a" and removes the 3 because it is repeated, how can I get this?
var c = ["4","5"]
Thank You!
You can use set and spread operators.
set has property to have all unique value object.
Below we are using Set constructor passing values using spread operator and it return an object so to get an array we are again passing that object with spread operator.
let a = ["1", "2", "3"]
let b = ["3", "4", "5"]
let c = [...new Set([...a, ...b])];
console.log(c);
Check if value exists on another array using includes()
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
You can remove values by using filter()
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
To do what you want:
const a = ["1","2","3"]
const b = ["3","4","5"]
const c = b.filter((value) => !a.includes(value))
This has been answered before and is in detail: How to get the difference between two arrays in JavaScript?
Related
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]
}))
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:
What is destructuring assignment and its uses?
(3 answers)
Closed 3 years ago.
I have set:
const [c, d ] = ["h", "i"]
in the browser console. It returns undefined.
After that, when I type c it returns "h",
and when I type d it returns "i".
To me it looks like an object where I have set the value of c and d according to ["h", "i"] 's order
I have been learning Javascript for the past 9 months. I am not sure what am I missing? Why it's behaving like this?
You are using destructuring - a great feature to directly get named access to nested properties of an Object or an Array:
const car= {
color: 'blue',
power: '120HP',
brand: 'an awesome one'
};
// Object Destructuring
const { colorOfCar, powerOfCar, brandOfCar} = car;
You are then directly able to work with each of these variable names.
It is more or less short for:
const colorOfCar = car.color;
const powerOfCar = car.power;
const brandOfCar = car.brand;
Hoping it is helpful!
Update:
An Array can be destructured the following way:
const rgb = [255, 200, 0];
// Array Destructuring
const [red, green, blue] = rgb;
Short for:
const red = rgb[0];
const green= rgb[1];
const blue= rgb[2];
You are using destructuring assignment. Then variable c = arr[0], d = arr[1]
If you need to copy array:
const newArr = arr.concat();
This statement
const [c, d ] = ["h", "i"]
is destructuring, which is not an array assignment. It doesn't read as
"an array having the elements c and d will be assigned the values of h
and i"
but it reads as
"The array having the values of h and i are destructured into
variables called c and d, respectively."
Hence, the result is not an array, nor an object, but two variables. You can use babeljs.io to check the equivalent of your code:
"use strict";
var c = "h",
d = "i";
If you are trying to set one array to equal another array all you need is:
const c = ["h", "i"];
const d = c;
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:
How to append something to an array?
(30 answers)
Closed 5 years ago.
var a = ["1", "2"];
var b = ["a", "b"];
var c = [];
//Can someone suggest how do i push a and b into c such that c is a 2-dimensional array? (By using JavaScript)
//c should look like [["1","2"],["a","b"]]
You are asking a very basic question, the answer is:
var a = ["1", "2"];
var b = ["a", "b"];
var c = []; // you could have written c = [a, b];
c.push(a, b);
It's quite simple. Just use push function and pass the array as an argument.
c.push(a);
c.push(b);
or push them together like: c.push(a,b);