For projects often get it, go with an array of array of objects and adding weight

For me, though the code has been knocked for six months, but still go heavy on the array and have an array of objects and adding shadows, so thanks to the write down

 

A, the addition of an array of objects

jsondata values:

jsonData = var [] 
var obj = []
jsonData = e.data var data_pie = new new the Array (); $ .each (jsonData, function (index, Item) { // hasOwnProperty: Is there a 'name' attribute lookup array obj IF (obj.hasOwnProperty (item.name, for)) { // If not, then let the value adding obj [item.name, for] + = (item.zongjinshui - item.zongchushui) / item.zongjinshui } the else { // if this property has direct assignment then obj [item.name, for] = (item.zongjinshui - item.zongchushui) / item.zongjinshui } });
console.log(obj)

Print obj values:

 

 The second cycle method

was jsondata = [] 
was obj = []
jsondata = e.data
                console.log(jsondata)
                var data_pie = new Array();
                $.each(jsondata, function(index, item) {
//第二种方法
if(obj[item.name]) { obj[item.name] += (item.zongjinshui - item.zongchushui) / item.zongjinshui } else { obj[item.name] = (item.zongjinshui - item.zongchushui) / item.zongjinshui } }); console.log(obj)

Where the value of jsondata and obj is the same.

  If only to get an array of words, this step has been good, but my project is sometimes required an array of objects,

// by traversing the array obj, the obj property assignment 
                $ .each (obj, function (index, item) { 
// here need to print their own look and value index item of what are, do not give the value of the property plus the wrong data_pie .push ({
' value ' : Item, ' name ' : index }) }); the console.log (data_pie)

Print data_pie:

 

   At this point, adding arrays and array object has been completed.

Second, an array of deduplication

  When the array has the same attributes, but does not require the same few, then consider going heavy, leaving only a

//1 reduce去重,针对数组对象
let person = [
      {id: 0, name: "A"},
      {id: 1, name: "B"},
      {id: 2, name: "C"},
      {id: 3, name: "D"},
      {id: 1, name: "E"},
      {id: 2, name: "F"},   
 ];

 let obj =} {; 
 The let Peon = person.reduce ((CUR, Next) => { 
     obj [next.id] ? "" : Obj [next.id] = to true && cur.push (Next);
      return CUR; 
 }, [ ]) // set default type array cur, and the initial value of the empty array 
the console.log (Peon);
  // 2 to the set weight for the pure element array 
 ARR = [the let . 1 , 2 , 2 ]; 
 the console.log ([... new new the Set (arr)]); // [1, 2] 

original link: HTTPS: // blog.csdn.net/qq_39045645/java/article/details/103383230

The de-emphasis I did not try to save the first down.

Guess you like

Origin www.cnblogs.com/wangrong-0823/p/12625535.html