如何使用Javascript从Array获取next和previous值

时间:2017-11-05 16:18:57

标签: javascript json

我需要一个帮助。我有一个包含一些值的json对象,我需要使用Javascript根据某些用户输入值找到下一个id和之前的id。我在下面解释我的代码。

var data = [{
    "parentdes": "Parent description1",
    "parentid":"1",
    "childdes": [{
      "des": 'chile description11',
      "childid":"11",
      "subchilddes": [{
        "des": 'subchild des111',
        "sub_sub_id":"111"
      },{
        "des": 'subchild des444',
        "sub_sub_id":"444"
      },{
        "des": 'subchild des555',
        "sub_sub_id":"555"
      }]
    }, {
      "des": 'chile description12',
      "childid":"12",
      "subchilddes": [{
        "des": 'subchild des112',
        "sub_sub_id":"112"
      }]
    }]
  }, {
    "parentdes": "Parent description2",
    "parentid":"2",
    "childdes": [{
      "des": 'chile description21',
      "childid":"21",
      "subchilddes": [{
        "des": 'subchild des212',
        "sub_sub_id":"212"
      }]
    }, {
      "des": 'chile description22',
      "childid":"22",
      "subchilddes": [{
        "des": 'subchild des222',
        "sub_sub_id":"222"
      }]
    }]
  }];

这里我有一个包含Json值的数组,如parent-child-subchild。假设用户输入了"sub_sub_id":"444"这里的输入,如果两者都可用,我需要获取next sub_sub_id and previous sub_sub_id,那么isNext=true and isPrevious=true也会检测到这两个ID。在这种情况下,输出应该与nextid=555 and previd=111isNext=true and isPrevious=true类似。类似地,如果用户输入是 - "sub_sub_id":"111",那么如果用户输入是 - nextid=444and previd=''则输出应该是isNext=true and isPrevious=false"sub_sub_id":"555",那么输出应该是{{1} }和nextid=112and previd=444。请帮帮我。

2 个答案:

答案 0 :(得分:0)

这应该有效,您可以使用ID拨打getInfo,然后您将获得prevnext ID

编辑:更新了代码,不对元素进行排序(错误)

    var data = [{
        "parentdes": "Parent description1",
        "parentid":"1",
        "childdes": [{
          "des": 'chile description11',
          "childid":"11",
          "subchilddes": [{
            "des": 'subchild des111',
            "sub_sub_id":"111"
          },{
            "des": 'subchild des444',
            "sub_sub_id":"444"
          },{
            "des": 'subchild des555',
            "sub_sub_id":"555"
          }]
        }, {
          "des": 'chile description12',
          "childid":"12",
          "subchilddes": [{
            "des": 'subchild des112',
            "sub_sub_id":"112"
          }]
        }]
      }, {
        "parentdes": "Parent description2",
        "parentid":"2",
        "childdes": [{
          "des": 'chile description21',
          "childid":"21",
          "subchilddes": [{
            "des": 'subchild des212',
            "sub_sub_id":"212"
          }]
        }, {
          "des": 'chile description22',
          "childid":"22",
          "subchilddes": [{
            "des": 'subchild des222',
            "sub_sub_id":"222"
          }]
        }]
      }];


    var index = {},
        prevChild;

    data.forEach(function(parent) {
      parent.childdes.forEach(function(child) {
        child.subchilddes.forEach(function(subchild) {
          if (prevChild) {
            prevChild.__next = subchild;
            subchild.__prev = prevChild;
          }
          index[parseInt(subchild.sub_sub_id, 10)] = subchild;

          prevChild = subchild;
        });
      });
    });

    function getInfo(id) {
      if (index[id]) {
        var res  = {},
            prev = index[id].__prev,
            next = index[id].__next;

        if (prev) res.prevId = prev.sub_sub_id;
        if (next) res.nextId = next.sub_sub_id;
        return res;
      }
    }

    console.log(getInfo(444)); // Print 111 and 555
    console.log(getInfo(555)); // Print 444 and 112

答案 1 :(得分:0)

这是逻辑:

  1. 遍历您的数组并将对象结构解析为代表sub_sub_id及其value的字符串。
  2. 如果您发现包含'sub_sub_id:'的字符串将其拆分并将值放在':'之后在一个新阵列
  3. 新数组将包含["111","444","555","112","212","222"](它实际上是每个ID的地图,其上一个和下一个值)。
  4. 使用新阵列,您可以轻松查看下一个sub_sub_id的内容,前一个sub_sub_id的内容以及每个内容是否存在 (我留下最后一部分供你写)
  5. 以下是执行此操作的代码(and a working codepan):

    var data = [{
        "parentdes": "Parent description1",
        "parentid":"1",
        "childdes": [{
          "des": 'chile description11',
          "childid":"11",
          "subchilddes": [{
            "des": 'subchild des111',
            "sub_sub_id":"111"
          },{
            "des": 'subchild des444',
            "sub_sub_id":"444"
          },{
            "des": 'subchild des555',
            "sub_sub_id":"555"
          }]
        }, {
          "des": 'chile description12',
          "childid":"12",
          "subchilddes": [{
            "des": 'subchild des112',
            "sub_sub_id":"112"
          }]
        }]
      }, {
        "parentdes": "Parent description2",
        "parentid":"2",
        "childdes": [{
          "des": 'chile description21',
          "childid":"21",
          "subchilddes": [{
            "des": 'subchild des212',
            "sub_sub_id":"212"
          }]
        }, {
          "des": 'chile description22',
          "childid":"22",
          "subchilddes": [{
            "des": 'subchild des222',
            "sub_sub_id":"222"
          }]
        }]
      }];
    
    var subIdsArray = [];
    
    function toObject(arr) {
      var rv = {};
      for (var i = 0; i < arr.length; ++i)
        if (arr[i] !== undefined) rv[i] = arr[i];
      return rv;
    }
    
    ObjectValues = function(v, k){
      if (typeof v == "object") {
        for (var kp in v) {
          if (Object.hasOwnProperty.call(v, kp)) {
            ObjectValues(v[kp], k != undefined ? k + "." + kp : kp);
          }
        }
      } else {
        var currentString = k + ":" + v;
        if(currentString.indexOf('sub_sub_id:') !== -1)
        {
          var valueToArray = currentString.split("sub_sub_id:")[1];
          subIdsArray.push(valueToArray);
        }
    
      }
    };
    
    for (var i in data) {
      ObjectValues(data[i]);
    }
    
    for (var i in subIdsArray) {
      console.log(subIdsArray[i]);
    }
    
    //subIdsArray = ["111","444","555","112","212","222"]
    

    只需从用户的输入中获取id并使用它检查数组中的键(如果id存在,请检查上一个和下一个键)。