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},
]
CodePudding user response:
Qais. Maybe you would 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 }) })