0

How can I match two array based on nameValue?

So in below example, delete array items which do not match based on nameValue and keep the productPrice:

    const allCombinations = ref([]);
    const allValuesForProduct = ref([]);
    //
    allCombinations.value = [
        {nameValue: 'blue/m/regular', productPrice: null},
        {nameValue: 'red/m/regular', productPrice: null},
        {nameValue: 'pink/m/regular', productPrice: null},
    ]

    allValuesForProduct.value = [
        {nameValue: 'blue', productPrice: 666},
        {nameValue: 'red', productPrice: 666},
        {nameValue: 'pink', productPrice: 666},
        {nameValue: 'blue/m/regular', productPrice: 99},
        {nameValue: 'red/m/regular', productPrice: 99},
        {nameValue: 'pink/m/regular', productPrice: 99},
    ]

End result should look like this:

    const allCombinations = ref([]);
    const allValuesForProduct = ref([]);
    //
    allCombinations.value = [
        {nameValue: 'blue/m/regular', productPrice: null},
        {nameValue: 'red/m/regular', productPrice: null},
        {nameValue: 'pink/m/regular', productPrice: null},
    ]

    allValuesForProduct.value = [
        {nameValue: 'blue/m/regular', productPrice: 99},
        {nameValue: 'red/m/regular', productPrice: 99},
        {nameValue: 'pink/m/regular', productPrice: 99},
    ]
1
  • 1
    you forgot the code you've tried - it's a simple filter/includes combination Commented Aug 18, 2022 at 7:55

1 Answer 1

2

Try this:

 allCombinations.value = allCombinations.value.filter(comb => {
   return allValuesForProduct.value.some(val => {
       return val.nameValue === comb.nameValue
   })
 })
 
 allValuesForProduct.value = allValuesForProduct.value.filter(val => {
   return allCombinations.value.some(comb => {
       return comb.nameValue === val.nameValue
   })  
 })
Sign up to request clarification or add additional context in comments.

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.