map

2023-04-12  本文已影响0人  swp小小喀嚓鱼
    var arr1 = [
        { name: '鸣人', age: 16 },
        { name: '佐助', age: 17 }
    ];

新增选项money,值为520,不改变原数组值arr1

    let a = arr1.map(item => {
        return { ...item, money: 520 }
    })
    console.log(a)
    console.log(arr1)
image.png

删除age属性,,不改变原数组值arr1

    let a = arr1.map(item => {
        const { age, ...rest } = item
        // return rest
        return { ...rest }
    })
    console.log(a)
    console.log(arr1)
image.png

改age属性的值为999,不改变原数组值arr1

    let a = arr1.map(item => {
        return { ...item, age: 999 }
    })
    console.log(a)
    console.log(arr1)
image.png

查name为[鸣人],把他的年龄变成99,不改变原数组值arr1

    let a = arr1.map(item => {
        if (item.name == '鸣人') {
            return { ...item, age: 999 }
        }
        return item
    })
    console.log(a)
    console.log(arr1)
image.png

把属性age,变成agegege

    let a = arr1.map(item => {
        return { ...item, 'agegege': item['age'] }
    }).map(item => {
        const { age, ...rest } = item
        return rest
    })
    console.log(a)
    console.log(arr1)
image.png
上一篇 下一篇

猜你喜欢

热点阅读