Checking if an array of objects contains value without looping? [duplicate] - javascript

This question already has answers here:
How to determine if Javascript array contains an object with an attribute that equals a given value?
(27 answers)
Closed 3 years ago.
Right now I have an array that looks like this
const array = [
{
value: 'received',
title: 'Hjá Birgja',
},
{
value: 'pending',
title: 'Yfirstandandi',
},
{
value: 'processing',
title: 'Í vinnslu',
},
]
and I would like this to return true
if(array.includes('processing'){
// do something
}

not exactly what you wanted since i'm not sure if you only wanted to search the value key but here's a solution
if (array.find(i => i.value === 'processing')) {
// do something
}

Related

What's wrong with this method? the .push() method isn't working as expected [duplicate]

This question already has answers here:
Array.push return pushed value?
(7 answers)
Closed 4 months ago.
const student1 = {
id: 1,
name: "Reed",
subjects: [],
addSubject(subject) {
this.subjects = this.subjects.push(subject); //what's wrong with this line
}
}
student1.addSubject('Math');
console.log(student1.subjects);
// logs out 1 instead of ['Math'], .push isn't functioning properly
const student1 = {
id: 1,
name: "Reed",
subjects: [],
addSubject: function(subject) {
this.subjects.push(subject);
}
}
student1.addSubject('Math');
console.log(student1.subjects);
Array.push() returns the new length of the array, not the array itself.
Unless you have a reason to capture this value, you don't need to assign it:
addSubject(subject) {
this.subjects.push(subject);
}

Adding key value to inner array in a multidimensional array when key doesn't exist (javascript) [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 months ago.
I need to develop a function whereby a type property gets appended to an array if it doesn't exist.
So the key will be of type and it should have a value of "dog".
Can someone kindly point out how I can iterate over the array to check if the key "type" exists and also provide guidance on how to append {type:"dog"} to the inner array if it doesn't exist. I tried animalArray[0][1]={type:"dog"} but it doesnt seem to work.
A typical array of animals will look like this:
labelTheDogs(
[
{name: 'Obi'},
{name: 'Felix', type: 'cat'}
]
)
// should return [
{name: 'Obi', type: 'dog'},
{name: 'Felix', type: 'cat'}
]
This is not a nested array
function labelTheDogs(dogs) {
dogs.forEach(dog => {
if (!dog.type) {
dog.type = 'dog'
}
})
return dogs
}
const dogs = labelTheDogs(
[{
name: 'Obi'
},
{
name: 'Felix',
type: 'cat'
}
]
)
console.log(dogs)
you can use the map function to do that
const newArray = yourArray.map((element) => {
if(!element.type){
element.type = "dog";
}else {
return element
}
})

how to get an object inside an array with a value [duplicate]

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 1 year ago.
Array looks list :
[
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
]
I have a value i.e Uid 673bjhbjhdcsy, how do I check if that Uid exists in the array and get the whole object associated with the Uid.
You can use find like:
const data = [
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
];
console.log(data.find(x => x.Uid === '673bjhbjhdcsy'));
Reference:
Array.prototype.find()
result = [
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
].find(item => item.Uid === '673bjhbjhdcsy')

Access part of an object javascript by id [duplicate]

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 1 year ago.
I have this object :
const list = {
id: 5,
name: "customer name",
projects: [
{
id:2,
title: "My project One",
studies: []
},
{
id:4,
title: "My project Two"
}
]
}
And I would like to be able to access a particular project in this object. There are only two of them here, but we imagine there could be more. How can I access a particular project by its ID? How can i do this ?
Thanks
You mention in the comments that the data is returned from an API. You can remap that data when it is received so that rather than a list of projects, it is an object keyed by the id field, which I assume is unique.
const allProjects = list.projects.reduce(
(collected, current) => {
return { ...collected, ...current };
}, {})
Failing that, you could use Array's filter to find the project you require:
const idToFind = 4;
const project = list.projects.filter( project => project.id === idToFind );

loop through array of objects using ramda [duplicate]

This question already has answers here:
How to use Ramda to find matching object in Array by key value
(2 answers)
How to find first element of array matching a boolean condition in JavaScript?
(14 answers)
Closed 3 years ago.
I have a array of objects this way.
0:
cardLast4Digits: "0664"
cardType: "GIFT_CARD"
__proto__: Object
1:
cardLast4Digits: "5551"
cardType: "CREDIT_CARD"
__proto__: Object
I want to loop through this array of object and find if cardType is "GIFT_CARD". Once i find it, i want to get that object as a result. Output should then be
0:
cardLast4Digits: "0664"
cardType: "GIFT_CARD"
__proto__: Object
Can someone please suggest me how to do this withv ramda.
Just use the array find method: https://ramdajs.com/docs/#find
Ramda:
const items = [{
cardLast4Digits: '0664',
cardType: 'GIFT_CARD'
}, {
cardLast4Digits: '5551',
cardType: 'CREDIT_CARD'
}];
R.find(R.propEq('cardType', 'GIFT_CARD'))(items);
ES6:
const items = [{
cardLast4Digits: '0664',
cardType: 'GIFT_CARD'
}, {
cardLast4Digits: '5551',
cardType: 'CREDIT_CARD'
}];
const result = items.find(item => item.cardType === 'GIFT_CARD');
console.log(result);

Categories

Resources