[英]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 个步骤:
terms
terms
对数据进行分组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());
Array#reduce
and Map
Array#reduce
和Map
terms
in a Map
.Map
中的terms
对数据进行分组。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.