Finding match element with array reduce [duplicate] - javascript

This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 2 years ago.
I have two arrays
a. [1,2,3,4,5]
b. [2,3,4,5,6]
I try to find 2,3,4,5 with array.reduce because I think it is more efficient.
Can I do so?

This will get you the same result without using reduce:
var a=[1,2,3,4,5];
var b= [2,3,4,5,6];
result = a.filter(p=>b.includes(p));
console.log(result);
Or with reduce:
var a=[1,2,3,4,5];
var b= [2,3,4,5,6];
var result = b.reduce((acc,elem)=>{
if(a.includes(elem)) acc.push(elem);
return acc;
},[]);
console.log(result);

With filter and includes
{
const a = [1,2,3,4,5];
const b = [2,3,4,5,6];
let overlap = a.filter(e => b.includes(e))
console.log(overlap)
}

Related

Filter array of objects from another array of objects [duplicate]

This question already has answers here:
How to get the difference between two arrays of objects in JavaScript
(22 answers)
Comparing two arrays of objects, and exclude the elements who match values into new array in JS
(6 answers)
Closed 17 days ago.
From the given array of objects how to filter the expected output
let a = [{name:'Hari',age:2},{name:'Chana',age:4},{name:'Like',age:5}]
let b = [{name:'Chana',age:14},{name:'Like',age:15}];
I tried this but not working;
let c =a.filter(elm => b.find(el => el.name === elm.name));
expected output is [{name:'Hari',age:2}]
You need to change little modification inside code of filter and final code will be :
let a = [{name:'Hari',age:2},{name:'Chana',age:4},{name:'Like',age:5}]
let b = [{name:'Chana',age:14},{name:'Like',age:15}];
let c = a.filter(elm => !b.find(el => el.name === elm.name));
console.log(c);
Result will be:
[ { name: 'Hari', age: 2 } ]
Check if the result of filter === 0
let a = [{name:'Hari',age:2},{name:'Chana',age:4},{name:'Like',age:5}]
let b = [{name:'Chana',age:14},{name:'Like',age:15}];
let c = a.filter(x => b.filter(y => y.name === x.name).length === 0);
console.log(c);

array.push() and splite in Array of arrays [duplicate]

This question already has answers here:
Split array into chunks
(73 answers)
Closed 6 days ago.
This post was edited and submitted for review 6 days ago.
I'm using arrayofArrays[0].push() inside a for loop in order to add an Object to the Array0 inside arrayofArrays.
Now I'd like that when Array0 has reached 2 element, the next element will be pushed to Array1, in order to achieve this situation:
var arrayofArrays = [[Obj0,Obj1],[Obj2,Obj3],[Obj4,Obj5], ...];
Sample code:
var arrayofArrays = [[]];
for(data in Data){
var Obj = {"field1":data[0], "field2":data[1], "field3":data[2] }
arrayofArrays[0].push(Obj); // need to pass to arrayofArrays[1] when arrayofArrays[0] has 2 Obj...
}
(I don't need to split an existing array, I'm adding Object to an array, and want them to split in sub-arrays while adding them)
Here is a functional programming approach to your question to unflatten an array:
const arr = Array.from(Array(6).keys()); // [0, 1, 2, 3...]
let t;
let arrOfArr = arr.map((v, idx) => {
if(idx % 2) {
return [t, v];
} else {
t = v;
return null;
}
}).filter(Boolean);
console.log({
arr,
arrOfArr,
});
Note: Array.from(Array(6).keys()) is just for demo. Replace this with any array of objects you like

JavaScript Second Largest Element in array Error in code [duplicate]

This question already has answers here:
Why doesn't the sort function of javascript work well?
(5 answers)
Closed 2 years ago.
I wrote this code to get second max...it worked in cases like nums=[1,2,3,2,4] and i got second max equal to 3.But if mu array is [1,2,3,4,5,6,7,8,9,10] the output for second max is 8.
Please help.
function getSecondLargest(arr){
let uniqueArr = [ ...new Set(arr) ];
uniqueArr.sort();
const z= uniqueArr.length;
return arr[z-2];
}
try this
let intArray =[1,2,3,4,5,6,7,8,9,10];
console.log(intArray.sort((a, b) => b - a)[1]);
Try this:
let arr = [1,2,3,4,5,6,7,8,9,10];
function getSecondLargest(nums){
let sort = arr.sort((a, b) => a - b);
return sort[sort.length - 2]
}
console.log(getSecondLargest(arr));
It is returning 8 because sort() method in javascript by default sorts the array in alphabetical order, so applying sort() method to your will return something like this:-
[1,10,2,3,4,5,6,7,8,9]
So, in order to sort them numerically, you've to add this new method which is further simplified by using arrows functions in ES6. Following will be your updated code:-
function getSecondLargest(arr){
let uniqueArr = [ ...new Set(arr) ];
uniqueArray.sort((a,b) => a-b);
const z= uniqueArr.length;
return arr[z-2];
}
It is sorting the elements alphabetically. Try passing the function in the sort:
let arr1 = [1,2,3,2,4];
let arr2 = [1,2,3,4,5,6,7,8,9,10];
console.log( getSecondLargest(arr1) );
console.log( getSecondLargest(arr2) );
function getSecondLargest(arr){
let uniqueArr = [ ...new Set(arr) ];
uniqueArr.sort((a,b)=> a-b);
const z= uniqueArr.length;
return arr[z-2];
}
I recommend you take care of edge cases such as arrays of length 0 or 1...

Find partial string in array [duplicate]

This question already has answers here:
How do you search an array for a substring match?
(15 answers)
Closed 3 years ago.
var fruits = ["CarPaint.InString.Banana", "CarPaint.InString.Orange", "CarPaint.InString.Apple", "CarPaint.InString.Mango"];
var n = fruits.includes("Mango");
Let's say you don't know whats inside the fruits array ?
How do you extract the string that contains mango.
The prefix string must be included in the result.
Can this be done without a for loop and parsing ?
var fruits = ["CarPaint.InString.Banana", "CarPaint.InString.Orange", "CarPaint.InString.Apple", "CarPaint.InString.Mango"];
var n = fruits.includes("Mango");
console.log(n)
You need to filter the array and check each string.
var fruits = ["CarPaint.InString.Banana", "CarPaint.InString.Orange", "CarPaint.InString.Apple", "CarPaint.InString.Mango"],
result = fruits.filter(string => string.includes("Mango"));
console.log(result);
How about the following?
fruits.filter(fruit => fruit.includes('Mango'))
// [ 'CarPaint.InString.Mango' ]

Concat javascript arrays as array of arrays [duplicate]

This question already has answers here:
How can I create a two dimensional array in JavaScript?
(56 answers)
Closed 5 years ago.
I have these arrays:
arr1 = ['29.5', 32035];
arr2 = ['30.5', 32288];
arr3 = ['31.5', 31982];
arr4 = ['1.6', 31768];
As a result I want to have something like this:
result = [['29.5', 32035], ['30.5', 32288], ['31.5', 31982], ['1.6', 31768]];
I means the result is array created by another arrays. The question is, how I can concat the arrays. result.push.apply(result, arr1); etc. give me array made by final values.
Thank you for any advice.
Actually you can just do
var result = [arr1, arr2, arr3, arr4];
Or
var result = [];
result[0] = arr1;
result[1] = arr2;
result[2] = arr3;
result[3] = arr4;
Or
var result = [];
result.push(arr1);
result.push(arr2);
result.push(arr3);
result.push(arr4);
You can do this using push .
var result = [];
result.push(arr1);
result.push(arr2);
result.push(arr3);
result.push(arr4);
console.log(result);

Categories

Resources