4

Is there a way to update a property in object array based on the number of times some other property is present as element in some other array

I have 2 arrays, array1 and array2:

var array1 = ["JOHN", "JACK", "JACK"]; var array2 = [ {count: 9, value: "JACK"}, {count: 9, value: "JOHN"}, {count: 2, value: "TEST"} ]; 

Expected output :

[ {count: 7, value: "JACK"}, // count = 9 - 2 {count: 8, value: "JOHN"}, // count = 9 - 1 {count: 2, value: "TEST"} ] 

In array1, "JACK" is present twice, so I need to reduce count by 2, similarly "JOHN" is present once and hence its reduced by 1, "TEST" is not present so unchanged.

I tried the following

array1.map(item => { return array2.find( p => p["value"] === item); }); 

With this, I am getting the below output,

[ {count: 9, value: "JOHN"}, {count: 9, value: "JACK"}, {count: 9, value: "JACK"} ] 

I am not sure whether it can be achieved using single lambda expression.

Thanks in advance!

3 Answers 3

1

You can get the result using array .map() and .filter() methods, assuming you are not allowed to change the original array:

var array1 = ["JOHN", "JACK", "JACK"]; var array2 = [{count: 9, value: "JACK"}, {count: 9, value: "JOHN"}, {count: 2, value: "TEST"}] var result = array2.map(({value, count}) => ({value, count: count - array1.filter(a=>a===value).length})) console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

  • Using .filter() check how many time a value is present in array1
  • Then subtract that result from current array2 count.
  • Then just return a new array of objects with the updated count.
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks ! that is exactly what i wanted
Hi Palash, I am a newbie here. Just wanted to confirm whether I have sucessfully marked this as answer ?
No problem, but no it is still not showing as the accepted answer. Maybe this link will help.
Yes, you are not a newbie anymore :-)
1

You can use reduce and map

  1. Loop over array1 and create a mapper object with names as key and repetition of names as value
  2. Loop over array2 and see if the name is present in mapper subtract respective value from

const array1 = ["JOHN", "JACK", "JACK"]; const array2 = [{count: 9, value: "JACK"},{count: 9, value: "JOHN"},{count: 2, value: "TEST"}] const mapper = array1.reduce((op, inp) => { op[inp] = op[inp] || 0; op[inp]++; return op; }, Object.create(null)) let final = array2.map(({count,value}) =>({ value, count: count - (mapper[value] || 0) })) console.log(final)

Comments

1

Assuming you are allowed to change the original array. ALSO assuming we are not talking 10s of thousand entries since I look up the value in the name array each time:

var array1 = ["JOHN", "JACK", "JACK"]; var array2 = [{count: 9, value: "JACK"}, {count: 9, value: "JOHN"}, {count: 2, value: "TEST"}] array2.forEach(item => item.count -= array1.filter(val => val === item.value).length); console.log(array2);

Less resources:

var array1 = ["JOHN", "JACK", "JACK"]; var array2 = [{count: 9, value: "JACK"}, {count: 9, value: "JOHN"}, {count: 2, value: "TEST"}] // create lookup table const names = array1.reduce((arr,cur) => { arr[cur] = (arr[cur]||0) + 1; return arr;},{}) // subtract if present array2.forEach(item => item.count -= (names[item.value] || 0)); console.log(array2);

4 Comments

On the cost of time complexity and side effects :)
How so? Do we care unless we have thousand of entries?
Yeah, I will not have many entries, at the max 100 in each array
@CodeManiac Ok, made a mapper too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.