-2
data: [
  {
    cid: "211211",
    parentCid: "212121",
    level: 3,
    number: "2.1.1",
    subject: "Title 2.1.1"
  },
  {
    cid: "111111",
    parentCid: "",
    lv: 1,
    number: "1",
    subject: "Title1"
  },
  {
    cid: "222222",
    parentCid: "",
    lv: 1,
    number: "2",
    subject: "Title2"
  },
  {
    cid: "212121",
    parentCid: "222222",
    lv: 2,
    number: "2.1",
    subject: "Title2.1"
  },
  {
    cid: "333333",
    parentCid: "",
    lv: 1,
    number: "3",
    subject: "Title3"
  }
]

I have a json data like above and I can get subject by data[i].subject.

For example, I can get "Title3" by data[i].subject.(i = 4) then I can get "Title3" prev item by data[i-1].subject and I will get "Title2.1".

My questionis , is there any way that I can get prev item in same "lv"(level)? in english is something like "data[ previtem in same lv] ?

Thanks and sorry for bad english.

3
  • write a code to sort the objects in the array in terms of that level. Then traverse and process. Commented Aug 11, 2015 at 4:31
  • ya..that is what I thought.I just want to make sure if there is another way can do it Commented Aug 11, 2015 at 4:33
  • An alternative solution, if you have control over how the data is generated and there is a limited number of levels, is to generate one array per level. Then looking up the previous item becomes trivial. Commented Aug 11, 2015 at 4:40

5 Answers 5

2

I've seen your previous question yesterday... I suppose that if you need to do it (search for spec element by level) for many times, you should sort the array. Or you just need to do it for few times, you just need a loop to find the previous element you want.

A simple method like this:

function findPrev(i) {
    var level = data[i--].lv;
    while (i >= 0) {
        if (data[i].lv === level) return i;
        i--;
    }

    return -1;  // not found
}

See the complete demo:

var data = [
    {
        cid: "211211",
        parentCid: "212121",
        lv: 3,
        number: "2.1.1",
        subject: "Title 2.1.1"
    },
    {
        cid: "111111",
        parentCid: "",
        lv: 1,
        number: "1",
        subject: "Title1"
    },
    {
        cid: "222222",
        parentCid: "",
        lv: 1,
        number: "2",
        subject: "Title2"
    },
    {
        cid: "212121",
        parentCid: "222222",
        lv: 2,
        number: "2.1",
        subject: "Title2.1"
    },
    {
        cid: "333333",
        parentCid: "",
        lv: 1,
        number: "3",
        subject: "Title3"
    }
];

function findPrev(i) {
    var level = data[i--].lv;
    while (i >= 0) {
        if (data[i].lv === level) return i;
        i--;
    }

    return -1;  // not found
}

console.log(data[findPrev(4)]); // data[4] go find data[2]

Sign up to request clarification or add additional context in comments.

Comments

1

I'm assuming you already have an index i of the "current item", or know how to get it. In that case, a naive method would be to move backwards from the current item in a loop, comparing lv:

var previousItemIndex = i - 1;

while(previousItemIndex >= 0 && data[i].lv !== data[previousItemIndex].lv)
    previousItemIndex--;

This will traverse the array backwards from i, finding the first element with a matching lv property, or returning -1 if it does not exist.

This also assumes that "previous" means in the same order as presented in the JSON array (i.e. the index has semantic meaning). If you meant to order by something else, for example number, you might consider sorting the entire thing first.

Comments

1

LateBloomer, The answer to your question is simple. To find the previous element with same level, all you need to do is decrement "i" and check whether the level is same or not. If not then decrement again.

Exactly what iplus26 did.

Comments

0

I think you want something like this:

function prevItemSameLv(data, i) {
    for (var idx=i-1; idx>=0; idx--) {
        if (data[idx].lv == data[i].lv) return data[idx];
    }
}

Comments

0

You can use library like _lodash, it has function named "findIndex", read more here: https://lodash.com/docs#findIndex But it will return only one value.

Also, you can check this question, about filtering data: lodash Filter collection using array of values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.