当前位置: 首页 > 知识库问答 >
问题:

javascript - 用js 写一方法,将a的数据处理为a1?

缪远
2023-07-19

id相同,ass里的数据根据 id相同,unicode 相同 ,把pearPlanFlag有属性的值加到第一个id出现的情况

const a = [    {      id: 3889,      ass: [        { unicode: 111, uniname: "上海", budgetMoney: 200 , pearPlanFlag: true},        { unicode: 222, uniname: "山西", budgetMoney: 600 , pearPlanFlag: false},        { unicode: 333, uniname: "江西", budgetMoney: 200 , },      ],    },    {      id: 3889,      ass: [        { unicode: 111, uniname: "上海", budgetMoney: 200 },        { unicode: 222, uniname: "山西", budgetMoney: 600 },        { unicode: 333, uniname: "江西", budgetMoney: 200 , pearPlanFlag: true},      ],    },    {      id: 4000,      ass: [        { unicode: 111, uniname: "上海", budgetMoney: 200 , pearPlanFlag: true},        { unicode: 222, uniname: "山西", budgetMoney: 600 },        { unicode: 333, uniname: "江西", budgetMoney: 200 },      ],    },    {      id: 4000,      ass: [        { unicode: 111, uniname: "上海", budgetMoney: 200 },        { unicode: 222, uniname: "山西", budgetMoney: 600 , pearPlanFlag: false},        { unicode: 333, uniname: "江西", budgetMoney: 200 },      ],    },    {      id: 4000,      ass: [        { unicode: 111, uniname: "上海", budgetMoney: 200 },        { unicode: 222, uniname: "山西", budgetMoney: 600 },        { unicode: 333, uniname: "江西", budgetMoney: 200 , pearPlanFlag: true},      ],    }];
const a1 = [    {      id: 3889,      ass: [        { unicode: 111, uniname: "上海", budgetMoney: 200 , pearPlanFlag: true},        { unicode: 222, uniname: "山西", budgetMoney: 600 , pearPlanFlag: false},        { unicode: 333, uniname: "江西", budgetMoney: 200 , pearPlanFlag: true },      ],    },    {      id: 3889,      ass: [        { unicode: 111, uniname: "上海", budgetMoney: 200 },        { unicode: 222, uniname: "山西", budgetMoney: 600 },        { unicode: 333, uniname: "江西", budgetMoney: 200 },      ],    },    {      id: 4000,      ass: [        { unicode: 111, uniname: "上海", budgetMoney: 200 , pearPlanFlag: true},        { unicode: 222, uniname: "山西", budgetMoney: 600 , pearPlanFlag: false},        { unicode: 333, uniname: "江西", budgetMoney: 200 , pearPlanFlag: true},      ],    },    {      id: 4000,      ass: [        { unicode: 111, uniname: "上海", budgetMoney: 200 },        { unicode: 222, uniname: "山西", budgetMoney: 600 },        { unicode: 333, uniname: "江西", budgetMoney: 200 },      ],    },    {      id: 4000,      ass: [        { unicode: 111, uniname: "上海", budgetMoney: 200 },        { unicode: 222, uniname: "山西", budgetMoney: 600 },        { unicode: 333, uniname: "江西", budgetMoney: 200 },      ],    }];

共有3个答案

蒋弘致
2023-07-19
Object.values(a.reduce((obj, {id, ass}) => {    let arr = obj[id] ??= {'': []};    arr[''].push({id, ass: ass.map(({pearPlanFlag: v, ...rest}) => {        (arr[rest.unicode] ??= rest).pearPlanFlag ??= v;        return rest;    })});    return obj;}, {})).flatMap(i => i['']);
刁瀚昂
2023-07-19
function transform(arr) {    const ret = [], cache = {};    for (let i = 0; i < arr.length; ++i) {        const { id, ass } = arr[i];        const _ass = (cache[id] ??= {});        ret[i] = { ...arr[i], ass: [] };        for (let j = 0; j < ass.length; ++j) {            const item = ret[i].ass[j] = { ...ass[j] };            const { unicode, pearPlanFlag } = item;            const _item = _ass[unicode];            if (!_item) {                _ass[unicode] = item;            } else if (pearPlanFlag != null) {                if (_item.pearPlanFlag == null) {                    _item.pearPlanFlag = pearPlanFlag;                }                delete item.pearPlanFlag;            }        }    }    return ret;}console.log(transform(a));
昌山
2023-07-19
function processArray(a) {    return a.map(item => {        const hasTrueFlag = item.ass.some(assItem => assItem.pearPlanFlag === true);        item.ass = item.ass.map(assItem => {            assItem.pearPlanFlag = hasTrueFlag;            return assItem;        });        return item;    });}const a1 = processArray(a);
 类似资料:
  • js:求一个数据处理方法,例如上述例子, tableList处理之后,可以得到 之前有如下方案 但是不能兼容account.t_sub_account_.*的场景,哪位大佬可以帮忙完善下吗?

  • js数据处理 const translations = { 'editor.remove': 'Remove', 'editor.copy': 'Copy', 'editor.words': 'WORDS', 'editor.characters': 'CHARACTERS', 'editor.default': 'Default', 'editor.recent': 'Recently Used

  • // 最大值是1000 let num = 1000; num是最大数为1000,然后根据arr数组里面的test的值之和(3+4+5+6)为18, 1000 / 18 = 55(求整数); 然后需要得到的格式排列的数据如下: 麻烦各位大佬帮忙看看,写了循环,排列的数据格式不对,双循环的排列按顺序的话要怎么写?

  • js 数组的数据处理 这个2数字不是固定的, 如果a这个值是2 我需要得到arr为 arr = [9] 如果a这个值是4 我需要得到arr为 arr = [7,8,9] 大佬们帮我看看

  • let num = 600; num是最大数为600,然后根据arr数组里面的test的值之和(5+4+5+6)为20, 600 / 20 = 30; 希望得到下面的格式: 图片第一列5个输入框对应数组arr里面下标为0的test的值为5 图片第二列4个输入框对应数组arr里面下标为1的test的值为4 ... 控制1-30之间的随机数里面的输入框修改的时候只能输入1-30的数, 控制31-60之

  • 学习js事件时,有三种不同的写法, 第一种 第二种 第三种 我想实现一下点击事件的防抖效果,但是直接在button标签中加点击事件没有效果。 第一个问题:第一种写法哪里有问题?应该怎么改正? 第二个问题:这三种写法是不是在运行的时候有区别?区别是什么? 希望有人解答一下