0

At the moment I have the following code, A JSON file, and with underscore I push the product ids of each look into one array, what I would like to do is push the product ids of each look into their own array, so I would have productArray1, productArray2, productArray3 etc. How could I adapt this?

JSON

looks = [{
        "id": "look1",
        "products": ["hbeu50271385", "hbeu50274296", "hbeu50272359", "hbeu50272802"]
    }, {
        "id": "look2",
        "products": [
            "hbeu50274106", "hbeu50273647", "hbeu50274754", "hbeu50274063", "hbeu50274911", "hbeu50274106", "hbeu50240022", "hbeu50271944"
        ]
    }, {
        "id": "look3",
        "products": [
            "hbeu50272935", "hbeu50274426", "hbeu50271624", "hbeu50274762", "hbeu50275366", "hbeu50274433", "hbeu50262002", "hbeu50272364", "hbeu50272359"
        ]
    }
    .......
]

JS (Underscore)

var productArray = []
_.each(looks, function(look) {
  _.each(look.products, function(product) {
    productArray.push(product.replace(/_.*/, ''))
  })
})

3 Answers 3

1

I may be misunderstanding, but are you just asking how to create an array of arrays?

var productArrays = [];
_.each(looks, function(look) {
  var productArray = [];
  _.each(look.products, function(product) {
    productArray.push(product.replace(/_.*/, ''))
  })
  productArrays.push(productArray);
});

For example: http://jsfiddle.net/bvaughn/acam2rzo/

This code will result in a 2D Array like so:

[
  ["hbeu50271385"1: "hbeu50274296"2: "hbeu50272359"3: "hbeu50272802"],
  ["hbeu50274106"1: "hbeu50273647"2: "hbeu50274754"3: "hbeu50274063"4: "hbeu50274911"5: "hbeu50274106"6: "hbeu50240022"7: "hbeu50271944"length],
  ["hbeu50272935"1: "hbeu50274426"2: "hbeu50271624"3: "hbeu50274762"4: "hbeu50275366"5: "hbeu50274433"6: "hbeu50262002"7: "hbeu50272364"8: "hbeu50272359"]
];
Sign up to request clarification or add additional context in comments.

3 Comments

I want an array with look1's product ids, an array with look 2's ids etc etc. so the array for look1 would have just these values: "hbeu50271385", "hbeu50274296", "hbeu50272359", "hbeu50272802"
so not an array of arrays, just a set of arrays.
A "set" is just a collection in which values don't repeat. Since you're only creating 1 array per product (as defined in the JSON) for this array-of-arrays, your outer array would be a set. Maybe I'm still half asleep, but I'm still not seeing how my above code snippet wouldn't return what you're asking for.
1

Let's build a list of [name, products] pairs and then pass that list to _.object to output an object having productArrayn as keys and the desired arrays of products as values :

var res = _.chain(looks).
    map(function(look) {
        return [
            "productArray" + look.id.substr(4),
            _.map(look.products, function(product) {
                return product.replace(/_.*/, '');
            })
        ];
    }).
    object().
    value();

would output

productArray1: ["hbeu50271385", "hbeu50274296", ...],
productArray2: ["hbeu50274106", "hbeu50273647", ...],
productArray3: ["hbeu50272935", "hbeu50274426", ...],
....

And a demo http://jsfiddle.net/nikoshr/fkmtksz5/

Comments

0

Something like this would work, productArrays will contain each of your product arrays, using their IDs as keys e.g: productArrays.look1 contains all of look1's products.

var productArrays = {};

_.each(looks, function (look) {
  productArrays[look.id] = [];

  _.each(look.products, function (product) {
    productArrays[look.id].push(product.replace(/_.*/, ''));
  });

});

Comments

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.