简体   繁体   English

如果内部对象为空,则删除对象数组

[英]Remove array of object if object inside is empty

I am preparing an array like this我正在准备这样的数组

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

where [5] is dynamic from response.其中 [5] 是动态的响应。

I have and object mydata and inside that I have a object items.我有和对象 mydata 并且在里面我有一个对象项目。

I push array to object items, with assign我将数组推送到对象项,并分配

Object.assign(mydatadata.items, datas);

Now mydata.items has an array set, `现在 mydata.items 有一个数组集,`

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 .现在如果 qty_auth: "" ,我需要从中检查 qty_ 是否为空,然后删除数组。 So expected output is something like this: Note: qty_ is dynamic here.所以预期的输出是这样的: 注意:qty_ 在这里是动态的。

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

and i want to result inside same object mydata.items我想在同一个对象 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但它现在给了我任何输出

 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);

You're talking about an array, but using curly brackets instead of square brackets.你在谈论一个数组,但使用大括号而不是方括号。 For filter() to work it would have to look like:为了 filter() 工作,它必须看起来像:

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.假设它是一个实际的数组,“const mydatadata.items”仍然存在问题,或者至少它会给我一个错误,因为 mydatadata 没有初始化。 Unless it's a typo and it should be mydata, but then you'd be redeclaring it.除非它是一个错字并且它应该是 mydata,但是你会重新声明它。 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.此外,您将结果存储在 mydatadata 中,但您正在记录变量结果。 So depending on the previous answer:所以根据前面的答案:

console.log(mydatadata);

or或者

console.log(mydata);

Here's a fiddle: https://jsfiddle.net/b57qa82d/这是一个小提琴: https ://jsfiddle.net/b57qa82d/

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.例如:“现在mydata.items有一个数组集”但您的代码说它应该是带有键的对象,而不是数组。

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.所以我建议:将您的数据放入一个数组中,并通过遍历每个对象的条目并检查是否有任何以“qty”开头的键的值不是空字符串来filter它。 You can then assign that filtered array to myObject.items .然后,您可以将该过滤后的数组分配给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);

Additional documentation附加文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM