loop through array of objects using ramda [duplicate] - javascript

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);

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
}
})

Get value from object in array: js [duplicate]

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 2 years ago.
This is what my array looks like:
const items = [
{ uuid: '123-1234-567', amountMoney: '20,02' },
{ uuid: '111-111-111', amountMoney: '44.04' }
]
And I have the uuid key in the variable:
const uuid = '111-111-111';
Now based on this uuid, I would like to extract the value from the amountMoney: 44.04.
How do you write this in a nice way in js?
You can use Array.prototype.find:
items.find(item => item.uuid === uuid) // -> found object
Use Array.prototype.find to find the object if the property uuid of the object matches the value of the variable uuid. Before extracting the value for amountMoney check if the object was found.
Example,
const items = [
{ uuid: '123-1234-567', amountMoney: '20,02' },
{ uuid: '111-111-111', amountMoney: '44.04' }
]
const uuid = '111-111-111';
const foundItem = items.find(item => item.uuid === uuid);
if (foundItem) {
console.log(foundItem.amountMoney)
}

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

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
}

Why does the console print an object in an array but not an object [duplicate]

This question already has answers here:
How to store objects in HTML5 localStorage/sessionStorage
(24 answers)
Closed 8 years ago.
I have the following:
var allItems = {Items: [
{Value: 1, Name: "String1"},
{Value: 2, Name: "String2"},
{Value: 3, Name: "String3"}
]
};
localStorage.setItem('allItems', allItems);
I'm struggling to access any properties of allItems after retrieving it from localStorage. How would I say access the Name of the second object in the array Items?
You have to use JSON stringify to store the object and then use parse to convert it back into a JSON object.
localStorage.setItem('allItems', JSON.stringify(allItems));
var storageItems = JSON.parse(localStorage.getItem('allItems'));
console.log(storageItems.Items[1].Name);

Categories

Resources