0

My goal: I want to modify this Array (See; Current Array with Objects) to only main properties where one or more (items) is greater than 0. In this example, the current Array should be modified by removing all object named: tags and sale_status from the Array, since there are no items is higher than 0 with count. So you only keep the Object over quotation_status.

How can I handle this the right way? I've tried various ways of looping, but can't figure it out.

Current Array with Objects

[{
    "id": "tags",
    "label": "Tags",
    "items": [{
        "key": 2,
        "count": 0
    }, {
        "key": 1,
        "count": 0
    }, {
        "key": 3,
        "count": 0
    }]
}, {
    "id": "sale_status",
    "label": "Status",
    "items": [{
        "key": "completed",
        "count": 0
    }, {
        "key": "processing",
        "count": 0
    }, {
        "key": "on-hold",
        "count": 0
    }]
}, {
    "id": "quotation_status",
    "label": "Status",
    "items": [{
        "key": "concept",
        "count": 1
    }]
}]

Desired output:

[{
    "id": "quotation_status",
    "label": "Status",
    "items": [{
        "key": "concept",
        "count": 1
    }]
}]

Current Script

function activeFilters() {
  let test = test1.value
  let filters = store.getters[test].filters

  const result = filters.filter((o) => o.items.some((obj) => obj.count));
  return result;
};

1 Answer 1

1

You can use filter and some to get the desired result

arr.filter((o) => o.items.some((obj) => obj.count))

const arr = [
  {
    id: "tags",
    label: "Tags",
    items: [
      {
        key: 2,
        count: 0,
      },
      {
        key: 1,
        count: 0,
      },
      {
        key: 3,
        count: 0,
      },
    ],
  },
  {
    id: "sale_status",
    label: "Status",
    items: [
      {
        key: "completed",
        count: 0,
      },
      {
        key: "processing",
        count: 0,
      },
      {
        key: "on-hold",
        count: 0,
      },
    ],
  },
  {
    id: "quotation_status",
    label: "Status",
    items: [
      {
        key: "concept",
        count: 1,
      },
    ],
  },
];

const result = arr.filter((o) => o.items.some((obj) => obj.count));
console.log(result);

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

6 Comments

Thanks! That works great. The only thing I get in console is: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'filter')
Add your full code, without code it is hard to tell what is the issue
I added the function script. For your information; The result works. I only get this wierd error.. It refers to const result = filters.filter((o) => o.items.some((obj) => obj.count)); specific filter
FYI: Error itself is telling you the problem, There is no method filter on undefined. I mean when you are calling filter on an object then that object at that particular time is undefined.
Thanks I understand now. I added a: If (filter) { } :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.