jQuery 从json数组中成对获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18103832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:01:54  来源:igfitidea点击:

get values in pairs from json array

javascriptjqueryajaxjson

提问by xprilion

Firstly, this is my json value i am getting from a php source:

首先,这是我从 php 源获取的 json 值:

[{"oid":"2","cid":"107"},{"oid":"4","cid":"98"},{"oid":"4","cid":"99"}]

After that, I want to get and oidvalue along with the corresponding cidvalue for example, oid=2and cid=107at one go, oid=4and cid=98at another and so on. I am trying to use jquery, ajax for this.

在那之后,我想和oid沿值与相应cid例如价值,oid=2并且cid=107一气呵成,oid=4cid=98在另一等等。我正在尝试为此使用 jquery、ajax。

I have tried many answers for this, like: Javascript: Getting all existing keys in a JSON arrayand loop and get key/value pair for JSON array using jQuerybut they don't solve my problem.

我为此尝试了很多答案,例如:Javascript:使用 jQuery 获取JSON 数组中的所有现有键循环并获取JSON 数组的键/值对,但它们并没有解决我的问题。

I tried this:

我试过这个:

for (var i = 0; i < L; i++) { var obj = res[i]; for (var j in obj) { alert(j); }

for (var i = 0; i < L; i++) { var obj = res[i]; for (var j in obj) { alert(j); }

but all this did was to return the key name, which again did not work on being used.

但这一切所做的只是返回键名,这在被使用时再次不起作用。

回答by tymeJV

So, you have an array of key/value pairs. Loop the array, at each index, log each pair:

所以,你有一个键/值对数组。循环数组,在每个索引处,记录每一对:

var obj = [{"oid":"2","cid":"107"},{"oid":"4","cid":"98"},{"oid":"4","cid":"99"}];

for (var i = 0; i < obj.length; i++) {
    console.log("PAIR " + i + ": " + obj[i].oid);
    console.log("PAIR " + i + ": " + obj[i].cid);
}

Demo: http://jsfiddle.net/sTSX2/

演示:http: //jsfiddle.net/sTSX2/

回答by woofmeow

This is an array that you have //lets call it a:

这是一个你拥有的数组 //让我们称之为:

[{"oid":"2","cid":"107"},{"oid":"4","cid":"98"},{"oid":"4","cid":"99"}]

[{"oid":"2","cid":"107"},{"oid":"4","cid":"98"},{"oid":"4","cid": “99”}]

To get first element :

获取第一个元素:

a[0]// this will give you first object i.e {"oid":"2","cid":"107"}

a[0]// 这会给你第一个对象,即 {"oid":"2","cid":"107"}

a[0]["oid"]// this will give you the value of the first object with the key "oid" i.e 2

a[0]["oid"]// 这会给你第一个键为“oid”的对象的值,即 2

and so on ...

等等 ...

Hope that helps. `

希望有帮助。`

回答by Diode

Basically what you need is grouping of objects in the array with a property. Here I am giving two functions using which you can do this

基本上,您需要的是使用属性对数组中的对象进行分组。在这里,我提供了两个函数,您可以使用它们来执行此操作

// To map a property with other property in each object. 
function mapProperties(array, property1, property2) {

    var mapping = {};

    for (var i = 0; i < data.length; i++) {
        var item = data[i];
        mapping[item[property1]] = mapping[item[property1]] || [];
        mapping[item[property1]].push(item[property2]);
    }

    return mapping;

}

// To index the items based on one property.
function indexWithProperty(array, property) {
    var indexing = {};

    for (var i = 0; i < data.length; i++) {
        var item = data[i];
        indexing[item[property]] = indexing[item[property]] || [];
        indexing[item[property]].push(item);
    }

    return indexing;
}

var data = [{
    "oid": "2",
    "cid": "107"
}, {
    "oid": "4",
    "cid": "98"
}, {
    "oid": "4",
    "cid": "99"
}];


var oidCidMapping = mapProperties(data, "oid", "cid");
console.log(oidCidMapping["2"]); // array of cids with oid "2"

var indexedByProperty = indexWithProperty(data, "oid");
console.log(indexedByProperty["4"]); // array of objects with cid "4"

May not be the exact solution you need, but I hope I am giving you the direction in which you have to proceed.

可能不是您需要的确切解决方案,但我希望我为您提供了必须继续的方向。

If you are willing to use other library you can achieve the same with underscorejs

如果您愿意使用其他库,您可以使用underscorejs实现相同的目的