2

It's my first post here :-)

Please, Can you a advice on this:

I have an object and an Array:

activeItems = {
    itemId: ["itemid1", "itemid2", "itemid3", "itemid4", "itemid5", "itemid6", "itemid7", "itemid8", "itemid9", "itemid10"],
    price: ["10.50", "22.10", "13.40", "11", "1100", "500", "100", "400", "500", "20"]
};
selectItems = ["itemid3", "itemid8", "itemid9"];

In the activeItems object price[0] represents the price for itemId[0].

All the prices are in correct order to represent prices for all item ids.

Now, I would like to create a new object with prices for the selecteItems array.

It should look like this:

newObject = {
     itemId: ["itemid3","itemid8","itemid9"],
     price: ["13.40", "400", "500"]
};

Basically, I'm looking for a formula that creates new object for selectedItems out of activeItems and adds prices arrays for them.

Thank you in advance!

Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
PAyTEK
  • 57
  • 7
  • Why don't you fix your data so it's easier to work with, like `{itemi1: 10.50, itemid2: 22.10, ...}`? – Barmar Apr 21 '18 at 10:17
  • 1
    @Barmar maybe that data is needed in that same structure as mentioned by the OP. Maybe in a chart or something similar that requires such structure – Ankit Agarwal Apr 21 '18 at 10:25
  • Maybe because I'm a beginner :) It will have 3000+ items and prices. Is it ok? Can you explain your method? You suggestt to use an object rather than two arrays in object? – PAyTEK Apr 21 '18 at 10:29

3 Answers3

2

Use a forEach loop and get the index of items from activeItems.itemId which can be used to get the corresponding price value from activeItems.price.

var activeItems = {
      itemId: ["itemid1", "itemid2", "itemid3", "itemid4", "itemid5", "itemid6", "itemid7", "itemid8", "itemid9", "itemid10"],
      price: ["10.50","22.10","13.40","11","1100","500","100","400","500","20"]
};

var selectItems = ["itemid3","itemid8","itemid9"];

var itemId = [];
var price = [];
selectItems.forEach(function(item){
  var index = activeItems.itemId.indexOf(item);
  itemId.push(item);
  price.push(activeItems.price[index]);
});

var newObject = {
  itemId: itemId,
  price : price
};

console.log(newObject);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

Maybe this is what you need: Key value arrays:

var myArray = {"itemid3": "13.40", "itemid8": "400", "itemid9": "500"};

And you add new items like this:

myArray = [];
myArray.itemid15 = "300";
Charis Moutafidis
  • 363
  • 1
  • 4
  • 17
0

Use the array reduce function

var activeItems = {
  itemId: ["itemid1", "itemid2", "itemid3", "itemid4", "itemid5", "itemid6", "itemid7", "itemid8", "itemid9", "itemid10"],
  price: ["10.50", "22.10", "13.40", "11", "1100", "500", "100", "400", "500", "20"]
};

var selectItems = ["itemid3", "itemid8", "itemid9"];
 
var newObject = selectItems.reduce(function(acc, curr, index) {
  // acc is the object passes as an argument
  // curr is current element
  // in this case curr will be "itemid3", "itemid8", "itemid9" ..so on
  acc.itemId.push(curr);
  // in next step get the index of the itemId3 etc from the original array
   // use this index to get the value from activeItems .price
  acc.price.push(activeItems.price[activeItems.itemId.indexOf(curr)])
  return acc;

}, {

  itemId: [],
  price: []
});
console.log(newObject)
brk
  • 48,835
  • 10
  • 56
  • 78