danedidug
danedidug

Reputation: 53

can't figure out how to use reduce properly

My goal is:

var arr = [{name: 'Alex'},{name: 'Mia'},{name: 'David'}];
bz(arr,'areWeAwesome','shuAr')

This should change arr to:

[{name: 'Alex', areWeAwesome: 'shuAr'},{name: 'Mia', areWeAwesome: 'shuAr'},{name: 'David', areWeAwesome: 'shuAr'}];

And I actually know how to do this:

function bz(zarr,zkey,zval) {
    for(var i in zarr) {
        zarr[i][zkey] = zval;
    }
}

But I can't figure it out how to implement this with reduce... I just don't see it. I tried many variations, the last one is this:

function bzz(zarr,zkey,zval) {
    return zarr.reduce(function(acc,val){
        return acc[val[zkey] = zval]
    },[{}]);
}

I read about reduce in the MDN documentation, but it didn't help... I just can't get it... would appreciate if you could help me with this

Upvotes: 3

Views: 45

Answers (4)

pkuzhel
pkuzhel

Reputation: 337

I would use map for this and not reduce.

The main reason, is that reduce is supposed to "reduce" things to one value (ie. add numbers) and not add things to arrays.

A map will iterate through an array, make any changes and return a new copy of that array with the changes for you.

But to answer your question, here is some code which will work for you:

function bzz(zarr,zkey,zval) {
    return zarr.reduce(function(acc,val){
        val[zkey]=zval;
        acc.push(val);
        return acc;
    }, []);
}

Let me know if you have any questions.

Upvotes: 0

Ele
Ele

Reputation: 33726

This is an alternative:

The function concat allows you to concatenate the previously elements with a new one, on the other hand, the function Object.assign assigns new properties to the current object.

function bz(arr, key, value) {
  return arr.reduce((a, c) => (a.concat(Object.assign({}, c, {[key]: value}))), []);
}

var arr = [{name: 'Alex'},{name: 'Mia'},{name: 'David'}];
console.log(bz(arr,'areWeAwesome','shuAr'));
.as-console-wrapper { max-height: 100% !important; top: 0; }

However, a better approach is using the function map:

function bz(arr, key, value) {
  return arr.map((c) => Object.assign({}, c, {[key]: value}));
}

var arr = [{name: 'Alex'},{name: 'Mia'},{name: 'David'}];
console.log(bz(arr,'areWeAwesome','shuAr'));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386560

With Array#reduce and Array#concat for a new object with the wanted key/value pair.

function bz(array, key, value) {
    return array.reduce((r, o) => r.concat(Object.assign({}, o, { [key]: value })), []);
}

var arr = [{name: 'Alex'},{name: 'Mia'},{name: 'David'}];
console.log(bz(arr,'areWeAwesome','shuAr'));

With Array#map, which is preferable, because it returns an array with a value for each item of the given array.

function bz(array, key, value) {
    return array.map(o => Object.assign({}, o, { [key]: value }));
}

var arr = [{name: 'Alex'},{name: 'Mia'},{name: 'David'}];
console.log(bz(arr,'areWeAwesome','shuAr'));

Upvotes: 3

brunnerh
brunnerh

Reputation: 184416

You really should not be using reduce for this...

var arr = [{name: 'Alex'},{name: 'Mia'},{name: 'David'}];

const bz = (arr, prop, value) =>
  arr.reduce((acc, e) => {
    acc.push({ [prop]: value, ...e });
    return acc;
  }, []);


console.log(bz(arr,'areWeAwesome','shuAr'));

Upvotes: 0

Related Questions