Get duplicates from array of objects by value [duplicate] - javascript

This question already has answers here:
How to combine an array in javascript
(3 answers)
Closed 6 years ago.
I have an array of objects:
var arr = [
{number: "AL-32021611", b: "7500"},
{number: "AL-32021612", b: "Continental"},
{number: "AL-32021612", b: "R3"},
{number: "AL-32021612", b: "7500"}
];
Is there a way that I can get all the number coincidences and get insted of number values the 'b' values in a var?
for example
//loop
result = ["Continental", "R3", "7500"]
what i want is for example i recive the number and then i search all the coincidences with that number value and what i exactly need is all the values from the coincidences

Using ES6 features:
let result = Array.from(new Set(arr.map(el => el.b)));
or
let result = [...new Set(arr.map(el => el.b))];
Array.from()
Set
Array.prototype.map()
Arrow Functions
Spread Operator ...

Str has a nice one-line answer for you, but you can also do it explicitly with a simple for loop. See below.
As you have it,
result = {"Continental", "R3" , "7500"};
is not a valid object. You could use a for loop and push the b values into a new array that would look like:
result = ["Continental", "R3" , "7500"];
Your for loop would look like:
var result = [];
for(var n of arr) {
result.push(arr[n].b);
}
return result;

Related

how to filter an array by index which is given as an array [duplicate]

This question already has answers here:
How to get an array of values based on an array of indexes?
(6 answers)
Closed 1 year ago.
I want to filter this array by index?
arr = [1,2,3,4,5,6];
I am trying to use
arr.filter(x => x === [2,3])
but it does not seems to work?
I want only [2, 3]rd index?
You need map here rather than filter:
arr = [11,22,33,44,55,66];
indexes = [2,3]
result = indexes.map(i => arr[i])
console.log(result)
Note that this returns values in the indexes order, but if indexes are unsorted and you need values in the source order, then you indeed need filter. Consider:
arr = [11,22,33,44,55,66];
indexes = [4,2]
result1 = indexes.map(i => arr[i])
result2 = arr.filter((_,i) => indexes.includes(i))
console.log(result1)
console.log(result2)

arr1 = [1, 2, 3] const { length } = arr1 is going to output 3, how is this works? [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 1 year ago.
so basically i found out that if we create array for example arr1 = [1, 2, 3] and new const { length } = arr1; will going to output 3 but how is this works exactly? is there any more operators instead of { length }? and what are the other use of using const { here some stuff } syntax? i do some research but i didnt find anything that points out this syntax. any assumptions?
Basically this is Object destructuring
Basic example:
const user = {
id: 42,
is_verified: true
};
const {id, is_verified} = user;
console.log(id); // 42
console.log(is_verified); // true
In your case:
const arr1 = [1, 2, 3]
arr1.length = 3
// Or,
const { length } = arr1
To know more: Object destructuring
Let examine each line of code :)
const arr1 = [1,2,3];
Here, you define an array "arr1", which has 3 elements, so arr1's length is 3.
In javascript, arr1 is an object, and 1 of its properties is "length", which reflects the length of the array. So, arr1.length = 3
Now the second line :
const {length} = arr1
Here, we use destructuring assignment to get the property "length" from object arr1. This line is equal to :
const length = arr1.length;
which give you length = 3
Make sense?
You can read more about the destructuring assignment here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Javascript multidimensional array update one value but got all updated [duplicate]

This question already has answers here:
How to create a 2d array of zeroes in javascript?
(5 answers)
Changing one array element affects other elements
(1 answer)
Closed 1 year ago.
I just found something is interesting in Javascript multidimensional array.
let arr = Array(3).fill([]);
arr[0].push('1');
I thought the result should be arr = [['1'], [], []], but got arr = [['1'], ['1'], ['1']].
If update a value as arr[2][0] = '*', expected array to be arr = [['1'], ['1'], ['*']], but got arr = [['*'], ['*'], ['*']].
So why Javascript multidimensional array work like this? How to just update a value in multidimensional array?
From pichard's comment, I got the right result by doing as following:
let arr = Array(3).fill([]).map(obj => { return [] });
arr[0].push('1');
arr[2][0] = '*';
The result is arr = [['1'], [], ['*']]

Merge 2 arrays in change [duplicate]

This question already has answers here:
Zip arrays in JavaScript?
(5 answers)
Closed 3 years ago.
I wanted to merge two arrays like you can fold 2 packs of cards - in ugly code with for loop and assuming same length so no if's for safety it would look like this:
const arr = [1,2,3];
const rra = [4,5,6];
const result = [];
for(let i=0; i<arr.length; i++){
result.push(arr[i],rra[i]);
}
console.log(result); // Array(6) [ 1, 4, 2, 5, 3, 6 ]
I know there is something similar in String.raw() but it cuts off last element and returns a string, is there equivalent in array ?
String.raw({raw: [1,2,3]}, ...[4,5,6]); //"14253"
You can use .flatMap() for this - This is the same as .map(), followed by .flat()
const arr = [1,2,3];
const rra = [4,5,6];
let newArray = arr.flatMap((ele, i) => [ele, rra[i]]);
console.log(newArray);

Copy property of each element of array1 into array2 [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 4 years ago.
I have array1 that contains 1 char in each element.
What I need, is to get the value of array1[i].charCodeAt(); and put it in array2.
Easy to do it, with a for statement.
for(i=0;i<10;i++){
y[i]= x[i].charCodeAt();
}
I did some research but nothing that explain this case:
Is it possible to populate array2 by some sort of destructuring, or what I am asking is not supported in js? For example:
array1 =['a','b','c'];
array2 = [];
array2 = array1[].charCodeAt.
conole.log('The first char has code ' + array2[0]); // The first letter has code 97.
You aren't creating separate standalone variables, so destructuring isn't what you're looking for - but, you can use .map to transform the first array into the second:
const array1 =['a','b','c'];
const array2 = array1.map(char => char.charCodeAt(0));
console.log(array2);
You can use Array.prototype.map():
const array1 = ['a','b','c'];
const array2 = array1.map(c => c.charCodeAt());
console.log(array2);

Categories