1

How do I populate filters.

let filters = {};

To make it look like this:

filters = {
  color: ["Blue", "Black"],
  size: [70, 50]
};

So if I would have a function:

function populate(key, value) {
    //if key is color value gets pushed to filters.color
    //if key is size value gets pushed to filters.size
    //if any other key, create new property with new name
}

So doing this would result in a new array in filters:

populate(material, "plastic");

And filters would look like this:

filters = {
  color: ["Blue", "Black"],
  size: [70, 50],
  material: ["plastic"]
};

3 Answers 3

3

You can use in to check if the key exists in the object. Based on that push the value in the object array otherwise creates a new key with array value.

let filters = {};
filters = {
  color: ["Blue", "Black"],
  size: [70, 50]
};

function populate(key, value) {
    //if key is color value gets pushed to filters.color
    //if key is size value gets pushed to filters.size
    //if any other key, create new property with new name
    if(key in filters)
      filters[key].push(value);
    else
      filters[key] = [value];
}
populate('material', "plastic");
console.log(filters);

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

Comments

1

You can use concat method of array if key present in filters Object otherwise it you will assign new array with new value.

DEMO

var filters = {
  color: ["Blue", "Black"],
  size: [70, 50]
};

function populate(key, value) {
  filters[key] = (filters[key]||[]).concat(value); 
}

populate('material', "plastic");
populate('color', "Red");
populate('size',55);
console.log(filters)
 .as-console-wrapper {max-height: 100% !important;top: 0;}

Comments

1

You could do something simple as this, where you check if the key exist and then push or assign.

array[key] ? array[key].push(value) : array[key] = [value];

Do note, you need to pass the key "material" as a string, and I as well recommend to pass the array too, to make the function more reusable.

filters = {
  color: ["Blue", "Black"],
  size: [70, 50]
};

function populate(a, k, v) {
  a[k] ? a[k].push(v) : a[k] = [v];
}

populate(filters, "material", "plastic");

console.log(filters)

8 Comments

won't that replace filters[key] with a new value every time the value will be material? I need to keep pushing new values to color, size or material
@Ciprian Updated again...somewhat more optimized, and reuseable
If you want to compress it a turnary might be good a[k] = a[k] ? a[k].push(v) : [v];
@AndrewBone That won't work... this one though a[k] ? a[k].push(v) : a[k] = [v];, so thanks
@AndrewBone Try it by calling populate 2 times and you'll see. After second populate the value is 2, not e.g. "plastic", "paper"
|

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.