0

I have an Array like this

var data = [
 {attribute_code: "description", value: "<p>Description</p>"}, 
 {attribute_code: "category_ids", value: Array(2)},
 {attribute_code: "required_options", value: "0"},
 {attribute_code: "has_options", value: "0"},
 {attribute_code: "activity", value: "11,18,19,20,21,22,23"},
 {attribute_code: "material", value: "37,38"}
]

Using lodash i would like to remove description, category_ids, required_options, has_options from it to look like

[
 {attribute_code: "activity", value: "11,18,19,20,21,22,23"},
 {attribute_code: "material", value: "37,38"}
]

I tried something like this

const filter = _(customAttributes)
    .keyBy('attribute_code')
    .pullAt(['description', 'category_ids', 'required_options', 'has_options'])
    .value();

But this is returning

[
 {attribute_code: "description", value: "<p>Description</p>"}, 
 {attribute_code: "category_ids", value: Array(2)},
 {attribute_code: "required_options", value: "0"},
 {attribute_code: "has_options", value: "0"},
]

as _.at, i guess its not mutating the array. What am i doing wrong here? I just cant figure it out.

2 Answers 2

1

Assuming that your original array is stored in data,

var data = [
 {attribute_code: "description", value: "<p>Description</p>"}, 
 {attribute_code: "category_ids", value: Array(2)},
 {attribute_code: "required_options", value: "0"},
 {attribute_code: "has_options", value: "0"},
 {attribute_code: "activity", value: "11,18,19,20,21,22,23"},
 {attribute_code: "material", value: "37,38"}
]

You can filter out unwanted elements with filter function:

var toRemove = new Set([
     "description",
     "category_ids",
     "required_options",
     "has_options"
])

_(data).filter(e => !toRemove.has(e.attribute_code)).value()

Also, this can be done without lodash.

data.filter(e => !toRemove.has(e.attribute_code))
Sign up to request clarification or add additional context in comments.

1 Comment

That works. I get confused with all the methods and don't know which one to use at the end ty.
1

You can use dropWhile()

var data = [{attribute_code: "description", value: "<p>Description</p>"},{attribute_code: "category_ids", value: Array(2)},{attribute_code: "required_options", value: "0"},{attribute_code: "has_options", value: "0"},{attribute_code: "activity", value: "11,18,19,20,21,22,23"},{attribute_code: "material", value: "37,38"}],
    removeParameters = ['description', 'category_ids', 'required_options', 'has_options'],
    result = _.dropWhile(data, ({attribute_code}) => removeParameters.includes(attribute_code));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>

1 Comment

Oh nice... always like to see different ways. Thank you

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.