Array of Objects- Increment a column comparing with other column data

时间:2016-05-06 16:36:50

标签: javascript arrays database object

I have a very complex calculation at hand and I am finding it difficult to get it through. Here it is. I have a table.

Table 1: (Name, ID, Location and Sum are the columns of a table)

Name | table1_ID | Location | Sum
rick |  1        | Gorets   | 
alex |  3        | hebrew   |

Table 2: (City, ID, Sex are the columns of a table)

City | table2_ID | Sex
wew  | 34        | M
rfgf | 3         | F
dgff | 1         | M
hgfhg| 1  | F

Table 3: (Notice, ID, Flag are the columns of a table)

Notice | table3_ID | Flag
hiji   | 1         |  true
asas   | 1         |  false   

Final OUTPUT Needed for table 1 should be as follows.

Depending on the values of table 2 and table 3, table 1 should look like this

Name | table1_ID | Location | Sum
rick |  1        | Gorets   |  4
alex |  3        | hebrew   |  1

I will explain you what is the above thing I am trying to achieve. As you can see, the first table column sum is empty. These gets updates based on the entries found in table2 and table3.

Basically. The 'ID' in table1 is searched in 'ID' of table2 and table3. for eg. ID with value '1' is searched in table2 and table3. It finds out that the ID is present 4 times in the 2nd and 3rd table. So it will put the sum value as 4.

Similarly it will search for 3 in the other 2 tables and finds out that it has occurred only once, so it will update 1 in the 'sum' column for ID '3'.

I hope you have got what I am trying to achieve. If I do a console.log for the 3 tables data, this is the result:

Table 1 :- console.log(userinfo);

[Object][Object]
 [0-1]
   [0]: Object
     Name: 'rick'
     table1_ID: 1
     Location:Gorets
     Sum:
   [1]: Object
     Name: 'alex'
     table1_ID: 3
     Location:hebrew
     Sum:

Similar format of output for other 2 tables. Can someone please let me know how to update the Sum: with the calculation values mentioned above.

1 个答案:

答案 0 :(得分:1)

您可以使用一个对象来获取表3中ID的所有计数,并将其作为表2计数的输入,并将其用作查找以在表1中指定总和。



function count(r, a) {
    r[a.ID] = (r[a.ID] || 0) + 1;
    return r;
}

var table1 = [{ Name: 'rick', ID: 1, Location: 'Gorets', Sum: undefined }, { Name: 'alex', ID: 3, Location: 'hebrew', Sum: undefined }, { Name: 'foo', ID: 42, Location: 'bar', Sum: undefined }],
    table2 = [{ City: 'wew', ID: 34, Sex: 'M' }, { City: 'rfgf', ID: 3, Sex: 'F' }, { City: 'dgff', ID: 1, Sex: 'M' }, { City: 'hgfhg', ID: 1, Sex: 'F' }],
    table3 = [{ Notice: 'hiji', ID: 1, Flag: true }, { Notice: 'asas', ID: 1, Flag: false }];

table1.forEach(function (a) {
    a.Sum = this[a.ID] || '-';
}, table2.reduce(count, table3.reduce(count, Object.create(null))));

document.write('<pre>' + JSON.stringify(table1, 0, 4) + '</pre>');
&#13;
&#13;
&#13;

修改

  

如果不是ID作为所有3个表的列名,我们将table1_ID,table2_ID和table3_ID作为列名称。我需要对代码进行哪些更改请求?

&#13;
&#13;
function count(key) {
    return function (r, a) {
        r[a[key]] = (r[a[key]] || 0) + 1;
        return r;
    };
}

var table1 = [{ Name: 'rick', table1_ID: 1, Location: 'Gorets', Sum: undefined }, { Name: 'alex', table1_ID: 3, Location: 'hebrew', Sum: undefined }, { Name: 'foo', table1_ID: 42, Location: 'bar', Sum: undefined }],
    table2 = [{ City: 'wew', table2_ID: 34, Sex: 'M' }, { City: 'rfgf', table2_ID: 3, Sex: 'F' }, { City: 'dgff', table2_ID: 1, Sex: 'M' }, { City: 'hgfhg', table2_ID: 1, Sex: 'F' }],
    table3 = [{ Notice: 'hiji', table3_ID: 1, Flag: true }, { Notice: 'asas', table3_ID: 1, Flag: false }];

table1.forEach(function (a) {
    a.Sum = this[a.table1_ID] || '-';
}, table2.reduce(count('table2_ID'), table3.reduce(count('table3_ID'), Object.create(null))));

document.write('<pre>' + JSON.stringify(table1, 0, 4) + '</pre>');
&#13;
&#13;
&#13;