js怎么改变数组对象中的指定的值

[{
  path: '/demand',
  name: 'demand',
  meta: {
    icon: '_xuqiu',
    title: '需求'
  },
  component: Main,
  children: [{
    path: 'my_demand',
    name: 'my_demand',
    meta: {
      icon: '_icon_xuqiushenpi',
      title: '我的需求单'
    },
    component: () => import('@/view/demand/my_demand.vue')
  },
  {
    path: 'materiel_demand',
    name: 'materiel_demand',
    meta: {
      icon: '_wuliaox',
      title: '物料需求单'
    },
    component: () => import('@/view/demand/materiel_demand.vue')
  }]
  }]

可以根据下面的my_demand,改名对应上面的title吗?如上面的name为my_demand的meta的title改为1

            let json = [{
              'my_demand': '1',
            }, {
              'materiel_demand': '2',
            }]
阅读 8.4k
2 个回答
const jsonMap = json.reduce((map, it) => {
    Object.keys(it)
        .forEach(key => map[key] = it[key]);
    return map;
}, {});

data
    .flatMap(m => m.children)
    .forEach(m => {
        const title = jsonMap[m.name];
        if (title) {
            m.meta.title = title;
        }
    });

console.log(jsonMap);
console.log(JSON.stringify(data, null, 2));
let _json = json.reduce((obj, cur) => {
    let key = Object.keys(cur)[0];
    obj[key] = cur[key];
    return obj;
}, {});

function replaceObj(arr = []) {
    return arr.map(item => {
        if (_json[item.name]) {
            item.meta.title = _json[item.name];
        }
        if (item.children) {
            item.children = replaceObj(item.children);
        }
        return item;
    });
}
console.log(replaceObj(data));
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题