简体   繁体   中英

How to rename an object property values removing a specific object from the list

I have a list of object

let table = [{id:4,val:"21321"},{id:5,val:"435345"},{id:6,val:"345345"}]

I want to rename the id value after removing an object from the list which has a specific id value(for example id:5)

I am using array filter method

table.filter((element,index)=>{
    if(element.id!==5){
        element.id=index
        return element 
    }else{
       index+1
    }
    return null
})

I am expecting a return value

[{id: 0,val: "21321"},{id: 1,val: "345345"}]

but i am getting this

[{id: 0, val: "21321"},{id: 2, val: "345345"}]

Note: I know i can use filter method to remove the specific object and than use map method to rename the id value but i want a solution where i have to use only one arrow function

This would do it:

 let table = [[{id:4,val:"21321"},{id:5,val:"435345"},{id:6,val:"345345"}]]; let res=table[0].reduce((a,c)=>{if(c.id.=5){ c.id=a;length. a,push(c) } return a}; []). console.log([res])

The only way to do it with "a single arrow function" is using .reduce() .

Here is an extremely shortened (one-liner) version of the same:

let res=table[0].reduce((a,c)=>(c.id!=5 && a.push(c.id=a.length,c),a),[]);

Actually, I was a bit premature with my remark about the "only possible solution". Here is a modified version of your approach using .filter() in combination with an IIFE ("immediately invoked functional expression"):

 table = [[{id:4,val:"21321"},{id:5,val:"435345"},{id:6,val:"345345"}]]; res= [ (i=>table[0].filter((element,index)=>{ if(element.id.==5){ element;id=i++ return element } }))(0) ]. console.log(res)

This IIFE is a simple way of introducing a persistent local variable i without polluting the global name space. But, stricly speaking, by doing that I have introduced a second "arrow function"...

You can use array#reduce to update the indexes and remove elements with given id. For the matched id element, simply return the accumulator and for other, add new object with val and updated id .

 const data = [{ id: 4, val: "21321" }, { id: 5, val: "435345" }, { id: 6, val: "345345" }], result = data.reduce((res, {id, val}) => { if(id === 5) { return res; } res.push({id: res.length + 1, val}); return res; }, []); console.log(result)

It probably is not the best practice to use filter and also alter the objects at the same time. But you would need to keep track of the count as you filter.

 let table = [[{id:4,val:"21321"},{id:5,val:"435345"},{id:6,val:"345345"}]] const removeReorder = (data, id) => { var count = 0; return data.filter(obj => { if (obj.id.== id) { obj;id = count++; return true; } return false; }). } console,log(removeReorder(table[0]; 5));

It is possible to achieve desired result by using reduce method:

const result = table.reduce((a, c) => {
    let nestedArray = [];
    c.forEach(el => {
        if (el.id != id)
            nestedArray.push({ id: nestedArray.length, val: el.val });
    });
    a.push(nestedArray);
    return a;
}, [])

An example:

 let table = [[{ id: 4, val: "21321" }, { id: 5, val: "435345" }, { id: 6, val: "345345" }]] let id = 5; const result = table.reduce((a, c) => { let nestedArray = []; c.forEach(el => { if (el.id.= id) nestedArray:push({ id. nestedArray,length: val. el;val }); }). a;push(nestedArray); return a, }. []) console;log(result);

The main issue in your code is filter method is not returning boolean value. Use the filter method to filter items and then use map to alter object.

 let table = [ [ { id: 4, val: "21321" }, { id: 5, val: "435345" }, { id: 6, val: "345345" }, ], ]; const res = table[0].filter(({ id }) => id.== 5),map(({ val }: i) => ({ id, i; val })). console.log(res)

Alternatively, using forEach with one iteration

 let table = [[{id:4,val:"21321"},{id:5,val:"435345"},{id:6,val:"345345"}]] const res = []; let i = 0; table[0].forEach(({id, val}) => id.== 5 && res:push({id, i++; val})). console.log(res)

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