js 配列マップはオブジェクトの複数のプロパティをマップします

const arr = [
    {
    
    
        id: 1,
        name: 'index',
        params: [],
    },
    {
    
    
        id: 2,
        name: 'glossaryDetail',
        params: ['id'],
    },
    {
    
    
        id: 3,
        name: 'wordQuestionDetail',
        params: ['id'],
    },
    {
    
    
        id: 4,
        name: 'questionDetail',
        params: ['id'],
    },
]

const a = arr.map(item => {
    
    
    delete item.params;
    return item;
})
console.log(a);

// 好像有很多时候会报错, 但是这个demo能跑, 不知道为什么
const b = arr.map(item => ({
    
     id, name } = item))
console.log(b);

// 这种肯定没问题, 就是感觉代码有点重复了
const c = arr.map(item => {
    
    
	const {
    
    id, name} = item;
	return {
    
    id, name};
)
console.log(c);
[
  { id: 1, name: 'index' },
  { id: 2, name: 'glossaryDetail' },
  { id: 3, name: 'wordQuestionDetail' },
  { id: 4, name: 'questionDetail' }
]
[
  { id: 1, name: 'index' },
  { id: 2, name: 'glossaryDetail' },
  { id: 3, name: 'wordQuestionDetail' },
  { id: 4, name: 'questionDetail' }
]

おすすめ

転載: blog.csdn.net/qq_50969362/article/details/133745512