Home > Mobile >  Remove array of object if object inside is empty
Remove array of object if object inside is empty

Time:11-02

I am preparing an array like this

 datas[5] = { "qty_sized": "1", "resolution": "5", "status": "", "order": 1342 };

where [5] is dymanic from response.

I have and object mydata and inside that I have a object items.

I push array to object items, with assign

Object.assign(mydatadata.items, datas);

Now mydata.items has an array set, `

items{
1 {qty_auth: "", resolution: "4", status: "", order: "1495"},
5 {qty_sized: "1", resolution: "4", status: "", order: "1485"}
}`

Now if qty_auth: "" , from which i need to check if qty_ is empty then remove the array . So expected output is something like this: Note: qty_ is dynamic here.

items{ 5 {qty_sized: "1", resolution: "4", status: "", order: "1485"} }

and i want to result inside same object mydata.items

I tried something like this

const mydatadata.items  = mydata.items.filter((o) =>
            Object.keys(o).some((k) => k.startsWith("qty") && o[k])
);

console.log(result);

but its now giving me any output

CodePudding user response:

const obj = {
  items: {
    1: {qty_auth: "", resolution: "4", status: "", order: "1495"},
    5: {qty_sized: "1", resolution: "4", status: "", order: "1485"}
  }
};

obj.items = Object.fromEntries(
  Object.entries(obj.items)
  .filter(([_, item]) =>
    Object.keys(item).some(key => key.startsWith('qty_') && item[key])
  )
);

console.log(obj);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You're talking about an array, but using curly brackets instead of square brackets. For filter() to work it would have to look like:

mydata = {
   items: [
      {qty_auth: "", resolution: "4", status: "", order: "1495"},
      {qty_sized: "1", resolution: "4", status: "", order: "1485"}
   ]
}

Assuming it is an actual array there's still a problem with "const mydatadata.items", or at least it throws an error for me because mydatadata is not initialized. Unless it's a typo and it should be mydata, but then you'd be redeclaring it. So depending on what you want:

mydata.items = mydata.items.filter((o) =>
        Object.keys(o).some((k) => k.startsWith("qty") && o[k])
);

or

let mydatadata = {};
mydatadata.items = mydata.items.filter((o) =>
        Object.keys(o).some((k) => k.startsWith("qty") && o[k])
);

Furthermore you're storing the result in mydatadata but you're logging the variable result. So depending on the previous answer:

console.log(mydatadata);

or

console.log(mydata);

Here's a fiddle: https://jsfiddle.net/b57qa82d/

CodePudding user response:

You should probably just be using an array rather than an object. It's not really clear from your question what structure you need as you keep changing the terminology to describe your data. For example: "Now mydata.items has an array set" but your code says that it should be object with keys, not an array.

So I suggest: get your data in an array, and filter it by iterating over each object's entries and checking to see if any of the keys starting with "qty" has a value that isn't an empty string. You can then assign that filtered array to myObject.items.

const data = [
{ "qty_sized": "0", "resolution": "5", "status": "", "order": 2 },
{ "qty_auth": "", "resolution": "5", "status": "", "order": 3 },
{ "qty_auth": "1", "resolution": "5", "status": "", "order": 1342 },
{ "qty_sized": "", "resolution": "2", "status": "", "order": 1 },
{ "qty_sized": "1", "resolution": "1", "status": "", "order": 142 }];

const filtered = data.filter(obj => {
  return Object.entries(obj).find(([key, value]) => {
    return key.startsWith('qty') && value;
  });
});

const myObject = { items: filtered };

console.log(myObject);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Additional documentation

  • Related