1

I have a problem and I have looked at a few reduce questions on here and I just can't seem to wrap my head around a way to do this. I have an array of objects. Each object has the same properties, about 30 of them. Each of the properties has completely different types of information. I want to be able to create a new object/array easily. My example is below.

var array = [{parts: 12345, purchased: "yes", customerId: 12541},
             {parts: 12432, purchased: "no", customerId: 55514},
             {parts: 12345, purchased: "Yes", customerId: 44421}];

What I want to do is find a way to extract useful data depending on the type of information in the array. For example:

some_function(array) {
             ...
             return { parts: {uniquePart: 12345, timesListed: 2}};
             }
     }

I may also want to extend that returned object and count the number of times purchased was either yes or no. I have tried numerous approaches but I am thinking this is more a data model issue than a programming issue.

I am parsing this data off of strings of receipts. Once the plain text string is parsed I get a large 30 property object for each transaction. Some of the properties also are nested objects or arrays as well.

I want to correlate data across multiple transactions. Maybe I need to research a better way to approach this type of situation as a whole.

So I understand the question is a little vague but what I really want to know is what is the best way with the array given to end up with the following data structure:

{parts: {uniquePart: 12345, timeListed 2}}

I believe once I understand how to itterate through the nested array of objects and build the new object I can go from there. My current attempts using reduce have not yielded fruit.

array.reduce(acc,obj){return This is where I am a little lost}
2
  • Everything you describe is possible with chaining maps/filters and reduces, but you'll have to very explicitly define what outputs you'll need to create. Eg: I use a function that gets all unique values from an array, one that calculates occurences of object fields and then a 3rd that uses both functions to create the output structures. So define your outputs, write several simple reduces/maps, then reuse and combine. Commented Nov 4, 2015 at 16:31
  • this is a very vague question, you have to be more specific Commented Nov 4, 2015 at 16:33

1 Answer 1

2

This solution features Array.prototype.forEach, Object.keys and Array.prototype.map for a temporary object count and returns the wanted array with one object for every part.

function getCount(array) {
    var count = {};
    array.forEach(function (a) {
        count[a.parts] = (count[a.parts] || 0) + 1;
    });
    return Object.keys(count).map(function (k) {
        return { parts: { uniquePart: k, timesListed: count[k] } };
    });
}

var array = [{ parts: 12345, purchased: "yes", customerId: 12541 }, { parts: 12432, purchased: "no", customerId: 55514 }, { parts: 12345, purchased: "Yes", customerId: 44421 }];
document.write('<pre>' + JSON.stringify(getCount(array), 0, 4) + '</pre>');

Sign up to request clarification or add additional context in comments.

3 Comments

This was exactly what I was looking for. So if I understand the code correctly you create a temporary object count. This object contains something roughly similar to : { 12345: 0 or value + 1 ) you then run a map function which I am a little fuzzy on, but it basically takes the count object gets a list of keys, At this point Is where my lack of functional programming knowledge comes in. Could you explain the map function a little bit as you have used it here?
the map builds a new array. it returns for every element in the array - here the keys of the count object - a value. the value is in this case an object with one property parts and the wanted values.
Thanks so much, now I understand!

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.