0

我一直在寻找没有运气的解决方案。我有一个 JSON 对象:

arr = [
{
    "id": "1",
    "dataId" : "", 
    "rel": ""
}, 
{
    "id": "2",
    "dataId" : "", 
    "rel": ""
}, 
{
    "id": "3", 
    "dataId" : "", 
    "rel": ""   
}, 
{
    "id": "4",
    "dataId" : "1", 
    "rel": ""   
}];

我需要一种用 javascript 操作这个数组的方法(允许使用 jquery),所以当“arr[a].dataId”与“arr[b].id”匹配时,取“arr[a]”元素并插入“ arr[b]" 创建一个新数组,如下所示:

newArr = [
{
    "id": "1",
    "dataId" : "", 
    "rel" : [
        {
            "id": "4",
            "dataId" : "1"  
        }
    ]
}, 
{
    "id": "2",
    "dataId" : "", 
    "rel": ""
}, 
{
    "id": "3", 
    "dataId" : "", 
    "rel": ""   
}

];

我希望你明白我在问什么,任何帮助将不胜感激。提前致谢

4

3 回答 3

0

试试这个:

var arr = [
{
    "id": "1",
    "dataId" : "", 
    "rel": ""
}, 
{
    "id": "2",
    "dataId" : "", 
    "rel": ""
}, 
{
    "id": "3", 
    "dataId" : "", 
    "rel": ""   
}, 
{
    "id": "4",
    "dataId" : "1", 
    "rel": ""   
}];
$(arr).each(function(index,value){
     if(value.dataId)
     {
         $(arr).each(function(ind,val){
             if(val.id==value.dataId)
             {
                 if(val.rel)
                 {
                     val.rel.push({"id":value.id,"dataId":value.dataId});
                 }
                 else
                 {                   
                     val.rel=[{"id":value.id,"dataId":value.dataId}];                     
                 }
             }
         });
     }
});
console.log(arr);

小提琴 http://jsfiddle.net/7g23B/

于 2013-04-13T17:48:27.260 回答
0

这个确实移动了元素,并且在迭代数组时不操纵数组。也不需要扩展 Array.prototype。一个缺点是您将在数组中找到漏洞(元素的索引不会从 0 到 length-1),但据我所知,这在您的用例中并不重要。

var arr = [
{
    "id": "1",
    "dataId" : "", 
    "rel": ""
}, 
{
    "id": "2",
    "dataId" : "3", 
    "rel": ""
}, 
{
    "id": "3", 
    "dataId" : "", 
    "rel": ""   
}, 
{
    "id": "4",
    "dataId" : "1", 
    "rel": ""   
}];

var indexesToMoveMap = {};

$.each(arr, function(outerIndex, outerElem) {
    if (outerElem.dataId) { 
        $.each(arr, function(innerIndex, innerElem) {
            if (innerElem.id === outerElem.dataId) {
                indexesToMoveMap[outerIndex] = innerIndex;
            }
        });
    }
});

$.each(indexesToMoveMap, function(indexToMove, indexToMoveTo) {
    arr[indexToMoveTo].rel = arr[indexToMoveTo].rel || [];
    arr[indexToMoveTo].rel.push(arr[indexToMove]);
    delete arr[indexToMove];
});

console.log(arr);
于 2013-04-13T18:32:46.997 回答
-1

像这样的东西?

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

var arr = [
{
    "id": "1",
    "dataId" : "", 
    "rel": []
}, 
{
    "id": "2",
    "dataId" : "", 
    "rel": []
}, 
{
    "id": "3", 
    "dataId" : "", 
    "rel": []   
}, 
{
    "id": "4",
    "dataId" : "1", 
    "rel": []   
}];

var index = 0;
for ( var m = 0; m < arr.length; m++ ) {
    for ( var i = 0; i < arr.length; i++ ) {

       if ( arr[index].id === arr[i].dataId ) {
             arr[index].rel.push (arr[i]);  
             arr.remove(i);
       }

    }
    index++;
}

console.log(JSON.stringify(arr, null, 4))
于 2013-04-13T17:37:42.440 回答