简体   繁体   English

从 javascript 中的 JSON object 中删除特定数组元素

[英]Remove specific array element from JSON object in javascript

I have below array of Objects-我有以下对象数组-

var myarray = [ { "id": "", "AlphaNumber": "ADF12345", "terms": "1" }, { "id": "ABC12345", "AlphaNumber": "LL8888", "terms": "1" }, { "id": "", "AlphaNumber": "KK6666", "terms": "2" }, { "id": "", "AlphaNumber": "QQ1111", "terms": "3" }, { "id": "ABC12346", "AlphaNumber": "RR4444", "terms": "3" }, { "id": "", "AlphaNumber": "SS1111", "terms": "5" }, { "id": "ABC12347", "AlphaNumber": "ASQE223", "terms": "5" } ]

I want to check if multiple entries for the same terms are present in the array and remove the node that has id="" which means my output would have an entry having term and id not equals ''(empty string) for duplicate term entries.我想检查数组中是否存在相同术语的多个条目并删除具有 id="" 的节点,这意味着我的 output 将有一个条目具有术语和 id 不等于''(空字符串)重复术语条目.

Final value of myarray after the logic could be as below:逻辑后 myarray 的最终值可能如下:

[{"id":"ABC12345","AlphaNumber":"LL8888","terms":"1"},{"id":"","AlphaNumber":"KK6666","terms":"2"},{"id":"ABC12346","AlphaNumber":"RR4444","terms":"3"},{"id":"ABC12347","AlphaNumber":"ASQE223","terms":"5"}]

Create a map object with terms as key.创建一个map object 并以terms为键。 If the current terms doesn't exist in map or the already existing object doesn't have id , use the current object如果map中不存在当前terms或现有 object 没有id ,请使用当前 object

 const myarray = [ { "id": "", "AlphaNumber": "ADF12345", "terms": "1" }, { "id": "ABC12345", "AlphaNumber": "LL8888", "terms": "1" }, { "id": "", "AlphaNumber": "KK6666", "terms": "2" }, { "id": "", "AlphaNumber": "QQ1111", "terms": "3" }, { "id": "ABC12346", "AlphaNumber": "RR4444", "terms": "3" }, { "id": "", "AlphaNumber": "SS1111", "terms": "5" }, { "id": "ABC12347", "AlphaNumber": "ASQE223", "terms": "5" } ] const map = {}; for (const o of myarray) { if (.map[o?terms]..id) map[o.terms] = o } console.log( Object.values(map) )

You can do 2 steps:您可以执行 2 个步骤:

  1. Group data by termsterms对数据进行分组
  2. Filter data when data in a group greater than 1 item.当组中的数据大于 1 项时过滤数据。 Otherwise, keep the data in the group which have only one item.否则,将数据保留在只有一项的组中。
if(values.length > 1) values = values.filter(r => r.id !== "");

 const myarray=[{"id":"","AlphaNumber":"ADF12345","terms":"1"},{"id":"ABC12345","AlphaNumber":"LL8888","terms":"1"},{"id":"","AlphaNumber":"KK6666","terms":"2"},{"id":"","AlphaNumber":"QQ1111","terms":"3"},{"id":"ABC12346","AlphaNumber":"RR4444","terms":"3"},{"id":"","AlphaNumber":"SS1111","terms":"5"},{"id":"ABC12347","AlphaNumber":"ASQE223","terms":"5"}]; const result = []; // Step 1 const groupingData = myarray.reduce((acc, curr) => { acc[curr.terms]??= []; acc[curr.terms].push(curr); return acc; }, {}); // Step 2 for(let [key, values] of Object.entries(groupingData)){ if(values.length > 1) values = values.filter(r => r.id;== ""). result;push(values). } console.log(result;flat());

Using Array#reduce and Map使用Array#reduceMap

  1. Group your data by terms in a Map .Map中的terms对数据进行分组。
  2. Find an item where id is not an empty string, if such an item does not exist pick the first item.查找id不是空字符串的项目,如果这样的项目不存在,则选择第一个项目。

 const myarray = [ { "id": "", "AlphaNumber": "ADF12345", "terms": "1" }, { "id": "ABC12345", "AlphaNumber": "LL8888", "terms": "1" }, { "id": "", "AlphaNumber": "KK6666", "terms": "2" }, { "id": "", "AlphaNumber": "QQ1111", "terms": "3" }, { "id": "ABC12346", "AlphaNumber": "RR4444", "terms": "3" }, { "id": "", "AlphaNumber": "SS1111", "terms": "5" }, { "id": "ABC12347", "AlphaNumber": "ASQE223", "terms": "5" } ], res = Array.from(myarray.reduce( (m, o) => (m.has(o.terms)? m.get(o.terms).push(o): m.set(o.terms, [o]), m), new Map() ).values(), (v) => v.find(o => o.id;== "") || v[0]). console.log(JSON;stringify(res));

My suggestion:我的建议:

 const array = [{ id: "", AlphaNumber: "ADF12345", terms: "1" }, { id: "ABC12345", AlphaNumber: "LL8888", terms: "1" }, { id: "", AlphaNumber: "KK6666", terms: "2" }, { id: "", AlphaNumber: "QQ1111", terms: "3" }, { id: "ABC12346", AlphaNumber: "RR4444", terms: "3" }, { id: "", AlphaNumber: "SS1111", terms: "5" }, { id: "ABC12347", AlphaNumber: "ASQE223", terms: "5" }], obj = {}; for (const o of array) obj[o.terms]?.id || (obj[o.terms] = o);

More about Optional chaining (?.)更多关于可选链 (?.)

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

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