1

I am trying to reduce a JSON array. Inside the array are other object, I am trying to turn the attributes into their own array.

Reduce Function:

    // parsed.freight.items is path
    var resultsReduce = parsed.freight.items.reduce(function(prevVal, currVal){
        return prevVal += currVal.item
    },[])
    console.log(resultsReduce); 
    // two items from the array
    // 7205 00000
    console.log(Array.isArray(resultsReduce));
    // false

The reduce function is kind of working. It gets both item from the items array. However I am having a couple problems.

1) The reduce is not passing back an array. See isArray test

2) I am trying to make a function so I can loop through all of the attributes in the array the qty, units, weight, paint_eligable. I cannot pass a variable to the currVal.variable here

Attempting:

    var itemAttribute = 'item';
    var resultsReduce = parsed.freight.items.reduce(function(prevVal, currVal){
        // pass param here so I can loop through
        // what I actually want to do it create a function and 
        // loop  through array of attributes
        return prevVal += currVal.itemAttribute
    },[])

JSON:

var request = {
    "operation":"rate_request",
    "assembled":true,
    "terms":true,
    "subtotal":15000.00,
    "shipping_total":300.00,
    "taxtotal":20.00,
    "allocated_credit":20,
    "accessorials":
    {
        "lift_gate_required":true,
        "residential_delivery":true,
        "custbodylimited_access":false
    },
    "freight":
    {
        "items":
        // array to reduce
        [{
            "item":"7205",
            "qty":10,
            "units":10,
            "weight":"19.0000",
            "paint_eligible":false
        },
        {    "item":"1111",
            "qty":10,
            "units":10,
            "weight":"19.0000",
            "paint_eligible":false
        }],

        "total_items_count":10,
        "total_weight":190.0},
        "from_data":
        {
            "city":"Raleigh",
            "country":"US",
            "zip":"27604"},
            "to_data":
            {
                "city":"Chicago",
                "country":"US",
                "zip":"60605"
            }
}

Thanks in advance

2
  • do you like to get only the value from item in an array? or the sum of the items? Commented Dec 16, 2016 at 18:29
  • var key = ['item','qty', 'units','paint_eligible', 'weight']; var resultsReduce = new Object(); key.forEach(function(item){ resultsReduce[item] = parsed.freight.items.reduce(function(array, object) { return array.concat(object[item]); }, []); }) this is what I ended up going. Just posting for my notes Commented Dec 19, 2016 at 21:05

1 Answer 1

2

You may need Array#map for getting an array of items

var resultsReduce = parsed.freight.items.reduce(function (array, object) {
    return array.concat(object.item);
}, []);

The same with a given key, with bracket notation as property accessor

object.property
object["property"]
var key = 'item',
    resultsReduce = parsed.freight.items.reduce(function (array, object) {
       return array.concat(object[key]);
    }, []);
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, that does solve my first problem. I was using prev, curr.item or prev+curr.item but that was giving me the same results. But makes sense. Thanks!
i still do not know, what you want - an array or a single number?
Sorry, I want an array, the first answer you gave was correct. It give me [ '7205', '00000' ]. This is exactly what I want
I didn't see update. You answered my question in total

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.