0

I'm trying to collate some data. I would like to populate an array containing sub arrays, for example, I have some json data that I am iterating over:

{
    "name": "name1",
    "prices": "209.67"
},
{
    "name": "name1",
    "prices": "350"
},
{
    "name": "name2",
    "price": "195.97"
},

I would like to create an array that ends up looking something like the following:

myArray['name1']prices[0] = 209.67,
               prices[1] = 350,
       ['name2']prices[0] = 195.97

I thought that the code below would achieve what I wanted but it doesn't work. It throws an exception. It doesn't seem to recognise the fact that the prices are an array for a given index into the main array. Instead the prices appear at the same level as the names. I want the main array for a given name to contain an inner array of prices.. Does anybody have any idea how I could modify to make this work?

function doStuff() {
  var cryptoData = getData();

  var datasetValues = {};
  datasetValues.names = [];
  datasetValues.names.prices = [];

  for (var result = 0; result < cryptoData.length; result++) {

    var data = cryptoData[result];

    if (datasetValues.names.indexOf(data.cryptoname) === -1)
    {
      datasetValues.names.push(data.cryptoname);
    }

    // This works
    //datasetValues.names.prices.push(data.prices);

    // This doesn't!
    datasetValues.cryptoNames[data.cryptoname].prices.push(data.prices);
  }
}
5
  • Why the downvote? Commented Nov 5, 2018 at 17:24
  • do you have pricesor price? Commented Nov 5, 2018 at 17:27
  • Each json result contains a single price but there could be more than one occurrence of a given name as indicated in the json above? Commented Nov 5, 2018 at 17:30
  • is it a question to me? what format do you have then? Commented Nov 5, 2018 at 17:30
  • Hi Nina, Not it wasn't a question really, I was sort of asking whether I answered your question as I wasn't sure that I did.. Thanks for your help. Commented Nov 5, 2018 at 17:51

2 Answers 2

2

You could reduce the array by using an object and take a default object if the property is not set. Then push the price.

var data = [{ name: "name1", price: "209.67" }, { name: "name1", price: "350" }, { name: "name2", price: "195.97" }],
    result = data.reduce((r, { name, price }) => {
        r[name] = r[name] || { name, prices: [] };
        r[name].prices.push(+price);
        return r;
    }, Object.create(null));
    
console.log(result);

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

1 Comment

Thank you very much. I wasn't aware of the reduce function. It works perfect for my needs.
1

Try this

function parseData(input){
    return input.reduce(function(o,i){
        o[i.name] = {};
        if(!o[i.name]['prices']){
           o[i.name]['prices'] = [];
        }
        o[i.name]['prices'].push(i.prices);
        return o;
    },{});
}

3 Comments

Thank you very much. I wasn't aware of the reduce function. It works perfect for my needs. I'm sorry but I accepted the first answer but i'm pretty sure that both would work. Joe
you overwrite prices for the same name.
@NinaScholz Thanks for pointing out . I have update the answer.

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.