-3

如何仅将文本附加到数组的第一个值并将数组发送回服务器。这是我从服务器得到的响应

[{
    "12": [ "12", "1", "2", "3" ]
}, {
    "13": [ "13", "1", "2", "3" ]
}, {
    "14": [ "14", "1", "2", "3" ]
}, {
    "15": [ "15", "1", "2", "3" ]
}]

我希望我的数组现在是这样的,以便我可以将此数组传递给我的数据表。

var object = [{
    12: ["12 hrs", "1", "2", "3"]
}, {
    13: ["13 hrs", "1", "2", "3"]
}, {
    14: ["14 hrs", "1", "2", "3"]
}, {
    15: ["15 hrs", "1", "2", "3"]
}]

如何将 hrs 附加到第一个值?

4

2 回答 2

0

这是一种简单的方法...

// Parse the string response from the server into an object
var obj = JSON.parse(serverResponse);
// loop through the object, knowing that it's nested in the first index of an array "obj[0]"
for(id in obj[0]){
    // obj->arrayIndex->objectProperty->arrayIndex append " hrs"
    obj[0][id][0] += " hrs";
}
// to return this to the server, apply JSON.stringify(obj)
于 2013-09-13T12:17:17.510 回答
0

我将object您的问题固定为-

var objectArray = [{"12" : ["12", "1", "2", "3"]}, {"13" : ["13", "1", "2", "3"]},{"14" : ["14", "1", "2", "3"]},{"15" : ["15", "1", "2", "3"]}]

这就是您可以循环objectArray并以您想要的方式进行更改的方式-

for(var i=0;i<objectArray.length;i++)
{
    for(var key in objectArray[i])
    {
        objectArray[i][key][0]+=" hrs";
        break;
    }
}

console.log(objectArray)
于 2013-09-13T11:44:11.747 回答