How to Find First Element of Array Matching a Boolean Condition in JavaScript

How to find first element of array matching a boolean condition in JavaScript?

Since ES6 there is the native find method for arrays; this stops enumerating the array once it finds the first match and returns the value.

const result = someArray.find(isNotNullNorUndefined);

Old answer:

I have to post an answer to stop these filter suggestions :-)

since there are so many functional-style array methods in ECMAScript, perhaps there's something out there already like this?

You can use the some Array method to iterate the array until a condition is met (and then stop). Unfortunately it will only return whether the condition was met once, not by which element (or at what index) it was met. So we have to amend it a little:

function find(arr, test, ctx) {
var result = null;
arr.some(function(el, i) {
return test.call(ctx, el, i, arr) ? ((result = el), true) : false;
});
return result;
}
var result = find(someArray, isNotNullNorUndefined);

Return Boolean if element of first array is in element of second nested array

You're on the right track with some:

const result = firstArrayObject.some(
({number}) => !secondObject.numbers.includes(number)
);

const firstArrayObject = [{name: "sample", number: 23}, {name: "sample2", number: 25}];
const secondObject = {someAttribute: "bla", numbers: [23, 26, 27, 28]};

const result = firstArrayObject.some(({number}) => !secondObject.numbers.includes(number));

console.log(result);

How to find first n array items that match a condition without looping through entire array

Rather than putting a conditional and break inside a for loop, just add the extra length check in the for condition itself

const data = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"],
isValid = n => !(n%2),
res = [],
max = 5;

for (let i = 0; i < data.length && res.length < max; i++) {
isValid(data[i]) && res.push(data[i]);
}

console.log(res)

find FIRST occurrence of element from Array1 IN Array2, Take the Index that element is found at in Array2, and make a new Array

You first filter to remove duplicates on Array2, and then look for a match in Array1 regarding id's

const Array1 = [
{
id: '001',
school: "blue springs"
},
{
id: '002',
school: "sycamore hills"
},
{
id: '003',
school: "moreland ridge"
},
{
id: '004',
school: "grain valley"
}
]

const Array2 = [
{
id: '003',
participant: "Susan"
},
{
id: '001',
participant: "Henry"
},
{
id: '003',
participant: "Justin" // <---- if 003 exists do not duplicate the return array, this is my issue....
},
{
id: '004',
participant: "Jessica"
},
{
id: '002',
participant: "Carly"
},
{
id: '001',
participant: "Chloe" // <---- if 001 exists do not duplicate the return array, this is my issue....
}
]
const alreadyShown = {};
const res = Array2.filter( el => {
if (!alreadyShown[el.id]){
alreadyShown[el.id] = true;
return true;
}
return false;
}).map( x => Array1.find( y => x.id === y.id ) || x);

console.log(res)

How to get the first element of an array?

like this

alert(ary[0])

Find and remove first matching element in an array of Javascript objects

You could create your own Array class:

 class Members extends Array {
removeByClass(className) {
for(const [index, member] of this.entries())
if(member.class === className)
return this.splice(index, 1)[0];
}
}

Use it as

 const members = new Members([ {/*...*/}, {/*...*/} ]);
members.removeByClass("...");

PS: "class" is a very bad name as it is a reserved keyword

Javascript / React - getting first item in array that matches specific conditions

You may need filter here:

var data=[ { lesson: 1, title: "Welcome", slug: "welcome", active: 'active', breakDay: false, started_time: new Date(), finished_time: null, completed: true, sublesson: [ { lesson: 1.1, title: "Evaluation", slug: 'evaluation', completed: false, answers: [] } ], answers: [] }, { lesson: 2, title: "Example", slug: "example", active: 'active', breakDay: false, started_time: null, finished_time: null, completed: false, sublesson: [ { lesson: 2.1, title: "example2", slug: 'example2', answers: [] } ], answers: [] }];
var result = data.filter(({active, completed})=> active && !completed);
console.log(result);


Related Topics



Leave a reply



Submit