简体   繁体   中英

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.

Final value of myarray after the logic could be as below:

[{"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. If the current terms doesn't exist in map or the already existing object doesn't have id , use the current 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:

  1. Group data by terms
  2. Filter data when data in a group greater than 1 item. 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

  1. Group your data by terms in a Map .
  2. Find an item where id is not an empty string, if such an item does not exist pick the first item.

 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 (?.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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