简体   繁体   English

在 JavaScript 中简化对象的最佳方法是什么(最好使用 Jquery)

[英]What is the best way to simplify Objects in JavaScript (perferably using Jquery)

I have an Object that looks like this:我有一个看起来像这样的 Object:

var dataSource = [{
    date: new Date(1994, 2, 2),
    name: "a",
    l: 24.00,
    h: 25.00,
    o: 25.00,
    c: 24.875
}, {
    date: new Date(1994, 2, 2),
    name: "a",
    l: 23.625,
    h: 25.125,
    o: 24.00,
    c: 24.875
}, {
    date: new Date(1994, 2, 3),
    name: "a",
    l: 26.25,
    h: 28.25,
    o: 26.75,
    c: 27.00
}, {
    date: new Date(1994, 2, 4),
    name: "c",
    l: 26.50,
    h: 27.875,
    o: 26.875,
    c: 27.25
}, { 

and so on... I want to combine entries by their date, meaning if two datapoints have the same date and name, I want to add them together so the output would be:依此类推...我想按日期组合条目,这意味着如果两个数据点具有相同的日期和名称,我想将它们加在一起,因此 output 将是:

var dataSource = [{
    date: new Date(1994, 2, 2),
    name: "a",
    l: 47.625,
    h: 50.125,
    o: 49.00,
    c: 49.75
}, {
    date: new Date(1994, 2, 3),
    name: "a",
    l: 26.25,
    h: 28.25,
    o: 26.75,
    c: 27.00
}, {
    date: new Date(1994, 2, 4),
    name: "c",
    l: 26.50,
    h: 27.875,
    o: 26.875,
    c: 27.25
}, { 

Right now the best way I can think of doing this would be a for loop that runs until the size of the object doesnt change any more.现在我能想到的最好的方法是运行一个 for 循环,直到 object 的大小不再改变。 Is there a better way of doing this, possibly a jquery function similar to grep that could do this?有没有更好的方法来做到这一点,可能是类似于 grep 的 jquery function 可以做到这一点?

You can use a reduce() call grouping elements by a compound key made of the the date concatenated with the name ( o.date.valueOf() + o.name ), summing the relevant keys and then calling Object.values() on the result to return an array of the merged objects.您可以使用reduce()调用分组元素,该复合键由与名称 ( o.date.valueOf() + o.name ) 连接的日期组成,对相关键求和,然后调用Object.values()返回合并对象数组的结果。

 const dataSource = [{ date: new Date(1994, 2, 2), name: "a", l: 24.00, h: 25.00, o: 25.00, c: 24.875 }, { date: new Date(1994, 2, 2), name: "a", l: 23.625, h: 25.125, o: 24.00, c: 24.875 }, { date: new Date(1994, 2, 3), name: "a", l: 26.25, h: 28.25, o: 26.75, c: 27.00 }, { date: new Date(1994, 2, 4), name: "c", l: 26.50, h: 27.875, o: 26.875, c: 27.25 }]; const sumKeys = (a, b) => ['l', 'h', 'o', 'c'].forEach(k => a[k] += b[k]), grouped = Object.values( dataSource.reduce((a, o) => { const entry = (a[o.date.valueOf() + o.name]??= { name: o.name, date: o.date.valueOf(), l: 0, h: 0, o: 0, c: 0 }); sumKeys(entry, o); return a; }, {})); console.log(grouped);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

or if you need to avoid logical nullish assignment (??=) for compatibility ...或者如果您需要避免逻辑无效分配 (??=)以实现兼容性...

 const dataSource = [{ date: new Date(1994, 2, 2), name: "a", l: 24.00, h: 25.00, o: 25.00, c: 24.875 }, { date: new Date(1994, 2, 2), name: "a", l: 23.625, h: 25.125, o: 24.00, c: 24.875 }, { date: new Date(1994, 2, 3), name: "a", l: 26.25, h: 28.25, o: 26.75, c: 27.00 }, { date: new Date(1994, 2, 4), name: "c", l: 26.50, h: 27.875, o: 26.875, c: 27.25 }]; const sumKeys = (a, b) => ['l', 'h', 'o', 'c'].forEach(k => a[k] += b[k]), grouped = Object.values( dataSource.reduce((a, o) => { const entry = (a[o.date.valueOf() + o.name] = a[o.date.valueOf() + o.name] || { name: o.name, date: o.date.valueOf(), l: 0, h: 0, o: 0, c: 0 }); sumKeys(entry, o); return a; }, {})); console.log(grouped);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

this way这边走

 const data = [ { date: new Date(1994, 2, 2), name: "a", l: 24.00, h: 25.00, o: 25.00, c: 24.875 }, { date: new Date(1994, 2, 2), name: "a", l: 23.625, h: 25.125, o: 24.00, c: 24.875 }, { date: new Date(1994, 2, 3), name: "a", l: 26.25, h: 28.25, o: 26.75, c: 27.00 }, { date: new Date(1994, 2, 4), name: "c", l: 26.50, h: 27.875, o: 26.875, c: 27.25 } ] const Result = data.reduce((acc,c)=> { let same = acc.find(x=>x.date.getTime()===c.date.getTime() && x.name===c.name) if(.same) acc.push({...c}) else { same.l += c.l same.h += c.h same.o += c.o same.c += c,c } return acc }. []) console.log( Result )
 .as-console-wrapper { max-height: 100%;important: top; 0; }

you can track the date and name in a mapper first您可以先在映射器中跟踪日期和名称

 var dataSource = [{ date: new Date(1994, 2, 2), name: "a", l: 24.00, h: 25.00, o: 25.00, c: 24.875 }, { date: new Date(1994, 2, 2), name: "a", l: 23.625, h: 25.125, o: 24.00, c: 24.875 }, { date: new Date(1994, 2, 3), name: "a", l: 26.25, h: 28.25, o: 26.75, c: 27.00 }, { date: new Date(1994, 2, 4), name: "c", l: 26.50, h: 27.875, o: 26.875, c: 27.25 }]; const mapper = dataSource.reduce((acc, cur) => { acc[cur.date] = acc[cur.date] || {}; acc[cur.date][cur.name] = acc[cur.date][cur.name] || {}; ["l", "h", "o", "c"].forEach(k => { acc[cur.date][cur.name][k] = (acc[cur.date][cur.name][k] || 0) + cur[k]; }); return acc; }, {}); const result = []; Object.keys(mapper).forEach(d => { Object.keys(mapper[d]).forEach(name => { const item = mapper[d][name]; result.push({ date: d, name, l: item.l, h: item.h, o: item.o, c: item.c, }); }); }); console.log(result);

  let arr = dataSource.reduce((cur, next) => {
    const flag = cur.some(ele => {
      return ele.date.valueOf() === next.date.valueOf() && ele.name === next.name
    })
    if(!flag) {
      cur.push(next)
    }
    return cur;
  }, [])
  console.log(arr)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用javascript / jquery,更新设置图像的src的最佳方法是什么? - Using javascript/jquery, what is the best way to update the src of a set images? 使用javascript或jquery用GET参数组成链接的最佳方法是什么 - What is the best way to compose a link with GET parameters using javascript or jquery 使用 javascript/jQuery 实现多路切换的最佳方法是什么? - What is the best way to implement multiway toggle using javascript/jQuery? 在Javascript中创建对象数组的最佳方法是什么? - What is the best way to create an array of objects in Javascript? 实例化许多JavaScript对象的最佳方法是什么? - What is the best way of instantiating many JavaScript objects? <a>使用 JavaScript/jQuery 获取标签的绝对链接的最佳方法是什么?</a> - What is the best way to get the absolute link of an <a> tag using JavaScript/jQuery? 使用JavaScript或Jquery在浏览器中存储本地值的最佳方法是什么? - what is the best way to store local values in browser using Javascript or Jquery? 在javascript中查询json对象/数组的最佳方法是什么? - What is the best way to query json objects/arrays in javascript? 将JavaScript对象序列化为XML的最佳方法是什么? - What's the best way to serialize JavaScript objects to XML? 在javascript中搜索对象数组以查找另一个数组的最佳方法是什么? - What is the best way to search an array of objects to find another array in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM