1

i have an object as :

let sample = {cat: 10 , dog: 50 , snake: 25};

I have the array of object as :

let petarray = [
    {name: 'newcat' , quantity: 20},
    {name: 'oldcat' , quantity: 15},
    {name: 'razordog' , quantity: 10}
];

May I know how can I modify the petarray quantity such that if petarray[allindexes].name includes any of sample , then quantity = quantity * (respective number of sample )

for eg: petarray[0].name includes cat , the quantity should be modified as 20*(10)

any help on how to achieve is much appreciated ,TIA (hope it is clear)

  • please let me know for any more info or on what i can do to improve about this result

4 Answers 4

2

you can try this,

let sample = {cat: 10 , dog: 50 , snake: 25};
let petarray = [{name: 'newcat' , quantity: 20},{name: 'oldcat' , quantity: 15},{name: 'razordog' , quantity: 10}];
let sampleArrayKeys = Object.keys(sample)
petarray = petarray.map((p) =>{
 const key = sampleArrayKeys.find(sak => p.name.includes(sak))
 if (key) {
  p.quantity *= sample[key]
 }
 return p
})

console.log(petarray)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the solution mate , i have a weird situation where sample = {cat: 10 , dog: 50 , snake: 25 , venomsnake : 15} and the name:"africavenomsnake" , may i know how this to be matched only with venomsnake not snake
Difficult to define this case, but you can give a shot by considering value of substring of key with max length. In venomsnake and snake example, you can see africavenomsnake has venomsnake with more length than snake.
yeah mate , good point :) ....may i know how can i check this
2

let sample = {cat: 10 , dog: 50 , snake: 25};
let petarray = [{name: 'newcat' , quantity: 20},{name: 'oldcat' , quantity: 15},{name: 'razordog' , quantity: 10}];

petarray.forEach(pet => {    
    for (let sampleKey in sample) {
        if (sample.hasOwnProperty(sampleKey)) {
            if (pet.name.includes(sampleKey)) {
                pet.quantity *= sample[sampleKey];
            }
        }
    }
}, petarray)

console.log(petarray)

Comments

2

It can be done like this -:

let sample = {cat: 10 , dog: 50 , snake: 25};

const sampleKeys = Object.keys(sample);

petarray = petarray.map(item =>
    sampleKeys.find(key => item.name.includes(key))
    ? { ...item, quantity: item.quantity * sample[match] }
    : item;
)

3 Comments

Thanks for the solution mate :) , i have a weird situation where sample = {cat: 10 , dog: 50 , snake: 25 , venomsnake : 15} and the name:"africavenomsnake" , may i know how this to be matched only with venomsnake not snake
It will match with the first occurence in the key array, If we need it to match with venomsnake we can ensure the object is like this where venomsnake comes first {cat: 10 , dog: 50 , venomsnake: ##, snake: 25}; and not like this {cat: 10 , dog: 50 , snake: 25, venomsnake: ##};
Thanks for suggestion , i would try this way :)
1

You can create mapping and then just map this array:

let sample = {cat: 10 , dog: 50 , snake: 25};

let petarray = [
    {name: 'newcat' , quantity: 20},
    {name: 'oldcat' , quantity: 15},
    {name: 'razordog' , quantity: 10}
];

const keys = Object.keys(sample);
const mapping = petarray.reduce((a, {name})=>{
    a[name] = a[name] || {};
    let sampleKey = keys.find(k => name.includes(k));
    if (sampleKey)
        a[name] = sampleKey;
    return a;
}, {})


petarray = petarray.map(({name, quantity}) => ({ name,
    quantity: ((sample[mapping[name]] ? (quantity * sample[mapping[name]]) : quantity))}));

console.log(petarray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.