如何在JSON连接数据之间执行操作?

时间:2022-08-18 17:03:28

Suppose I have two JSON strings:

假设我有两个JSON字符串:

json1 = [{"hc-key":"ar-tf","value":221},{"hc-key":"ar-ba","value":10820},{"hc-key":"ar-sj","value":230}]

json2 = [{"hc-key":"ar-ba","value":2890151},{"hc-key":"ar-sj","value":15625084},{"hc-key":"ar-tf","value":367828}]

what I'd like to do is to join them by "hc-key", into a new JSON string, where the new "value" is the result of dividing the value in json1 by the value in json2. In JavaScript.

我想要做的是通过“hc-key”将它们连接到一个新的JSON字符串中,其中新的“值”是将json1中的值除以json2中的值的结果。在JavaScript中。

Note that the data in json2 is not in the same order as in json1, BUT they both have the exact same keys.

请注意,json2中的数据与json1中的数据顺序不同,但它们都具有完全相同的键。

The resulting JSON should be:

生成的JSON应该是:

 result = [{"hc-key":"ar-ba","value":10820/2890151},{"hc-key":"ar-sj","value":230/15625084},{"hc-key":"ar-tf","value":221/367828}]

(I've ommited the resulting values for clarification)

(我已经省略了结果值以便澄清)

thanks.

谢谢。

2 个解决方案

#1


3  

Robert is correct that joins are better server side but what you want to do is certainly possible just using some JavaScript loops.

罗伯特是正确的,连接是更好的服务器端,但你想要做的当然可以只使用一些JavaScript循环。

I did not bother with error handling for this example, but I would also recommend confirming you have real numbers before dividing and that you are not dividing by zero.

我没有为此示例中的错误处理而烦恼,但我还建议您在分割之前确认您有实数并且您没有除以零。

var json1 = [
  {"hc-key":"ar-tf","value":221},
  {"hc-key":"ar-ba","value":10820},
  {"hc-key":"ar-sj","value":230}];

var json2 = [
  {"hc-key":"ar-ba","value":2890151},
  {"hc-key":"ar-sj","value":15625084},
  {"hc-key":"ar-tf","value":367828}];

var result = [];

for(var i = 0; i < json1.length; i++)
{
    var key = json1[i]['hc-key'];
    for(var j = 0; j < json2.length; j++) {
        if(json2[j]['hc-key'] == key) {
            result.push({
                "hc-key":key,
                "value": json1[i]['value'] / json2[j]['value']
            });
            break;
        }
    }
}
    
//show results
var key, value;
var $results = $('.results tbody');

for(var i = 0; i < result.length; i++)
{
    key = result[i]['hc-key'];
    value = result[i]['value'];
    $results.append('<tr><td>' + key + '</td><td>' + value + '</td></tr>');
}
td {
  padding-right: 15px;
  text-align: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<b>results:<b>

<table class="results">
  <tbody></tbody>
</table>

#2


1  

Heres a functional way to do it. This way will also work with the ReactiveX observables for async programming.

这是一种功能性的方法。这种方式也适用于ReactiveX observables进行异步编程。

Array.prototype.concatAll = function() {
    var results = [];
    this.forEach(function(subArray) {
        results.push.apply(results, subArray);
    });

    return results;
};


result = json1.map(function(obj1) {
    return json2.
        filter(function(obj2) {return obj2["hc-key"] == obj1["hc-key"]}).
        map(function(obj2) {
        return {
            "hc-key": obj1["hc-key"],
            "value": obj1.value / obj2.value
        };
    });
}).concatAll()

#1


3  

Robert is correct that joins are better server side but what you want to do is certainly possible just using some JavaScript loops.

罗伯特是正确的,连接是更好的服务器端,但你想要做的当然可以只使用一些JavaScript循环。

I did not bother with error handling for this example, but I would also recommend confirming you have real numbers before dividing and that you are not dividing by zero.

我没有为此示例中的错误处理而烦恼,但我还建议您在分割之前确认您有实数并且您没有除以零。

var json1 = [
  {"hc-key":"ar-tf","value":221},
  {"hc-key":"ar-ba","value":10820},
  {"hc-key":"ar-sj","value":230}];

var json2 = [
  {"hc-key":"ar-ba","value":2890151},
  {"hc-key":"ar-sj","value":15625084},
  {"hc-key":"ar-tf","value":367828}];

var result = [];

for(var i = 0; i < json1.length; i++)
{
    var key = json1[i]['hc-key'];
    for(var j = 0; j < json2.length; j++) {
        if(json2[j]['hc-key'] == key) {
            result.push({
                "hc-key":key,
                "value": json1[i]['value'] / json2[j]['value']
            });
            break;
        }
    }
}
    
//show results
var key, value;
var $results = $('.results tbody');

for(var i = 0; i < result.length; i++)
{
    key = result[i]['hc-key'];
    value = result[i]['value'];
    $results.append('<tr><td>' + key + '</td><td>' + value + '</td></tr>');
}
td {
  padding-right: 15px;
  text-align: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<b>results:<b>

<table class="results">
  <tbody></tbody>
</table>

#2


1  

Heres a functional way to do it. This way will also work with the ReactiveX observables for async programming.

这是一种功能性的方法。这种方式也适用于ReactiveX observables进行异步编程。

Array.prototype.concatAll = function() {
    var results = [];
    this.forEach(function(subArray) {
        results.push.apply(results, subArray);
    });

    return results;
};


result = json1.map(function(obj1) {
    return json2.
        filter(function(obj2) {return obj2["hc-key"] == obj1["hc-key"]}).
        map(function(obj2) {
        return {
            "hc-key": obj1["hc-key"],
            "value": obj1.value / obj2.value
        };
    });
}).concatAll()