0

I have the following array:

    array-1:

        Array[11]
        0:"265"
        1:"text-success"
        2:"51"
        3:"text-warning"
        4:"16"
        5:"text-warning"
        6:"35"
        7:"text-warning"
        8:"38"
        9:"text-warning"
        10:"106"

    array-2:
        Array[11]
        0:"265"
        1:"text-success"
        2:"51"
        3:"text-warning"
        4:"16"
        5:"text-warning"
        6:"35"
        7:"text-success"
        8:"38"
        9:"text-warning"
        10:"106"

   array-3:
        Array[11]
        0:"265"
        1:"muted"
        2:"51"
        3:"text-warning"
        4:"16"
        5:"text-warning"
        6:"35"
        7:"text-success"
        8:"38"
        9:"text-warning"
        10:"106"

    array-4:
        Array[11]
        0:"265"
        1:"text-warning"
        2:"51"
        3:"text-warning"
        4:"16"
        5:"muted"
        6:"35"
        7:"text-warning"
        8:"38"
        9:"text-warning"
        10:"106"

1st one will return FALSE because both exist "text-success"-twice AND "muted"-once

2nd one will return TRUE because it exist ONCE "text-success"

3rd one will return FALSE because both exist "text-success" AND "muted"

4th one will return TRUE because it exist ONCE "muted"

I need to parse the array and get the result:

array-1:

    Array[11]
        0:"265"
        1:"text-success"
        2:"51"

array-2: Null

array-3: Null

array-4:

    Array[11]
        0:"265"
        1:"muted"
        2:"16"

so far I have this:

function singles( array) {
    for( var index = 0, single = []; index < array.length; index++ ) { 
        if(array[index] == "text-success" || array[index] == "muted") {
            single.push(array[index]);
        }
    }
    return single;
};

Please help anyone?

4
  • What problem are you actually trying to solve? This screams "ugly workaround-hack" to me. Commented Jul 25, 2016 at 13:16
  • @TomLord I am tring to parse array in Javascript Commented Jul 25, 2016 at 13:27
  • But... Why? What problem are you trying to solve? Commented Jul 25, 2016 at 15:04
  • That's not an Array but an object containing 4 sub-Arrays Commented Jul 25, 2016 at 18:29

2 Answers 2

1

It's not clear whether you have an object that contains multiple arrays, or just a standard one-dimensional array for which you've given several possible examples, but assuming the latter I think this is what you're looking for:

function singles(array) {
  var foundIndex = -1;
      
  for(var index = 0; index < array.length; index++) {
    if(array[index] === "text-success" || array[index] === "muted") {
      if (foundIndex != -1) {
        // we've found two, so...
        return null;
      }
      foundIndex = index;
    }
  }
  if (foundIndex != -1) {
    return [ array[0], array[foundIndex], array[foundIndex+1] ];
  }
  return "Some default value for when it wasn't found at all";
}

console.log(singles(["532","text-warning","51","text-warning","16","muted","35","text-warning","38","text-warning","106"]));
console.log(singles(["533","text-warning","51","text-warning","16","muted","35","text-success","38","text-warning","106"]));

This loops through the input array. The first time "text-success" or "muted" is found its index is stored in foundIndex.

If "text-success" or "muted" is found a second time we immediately return null.

If we get to the end of the loop then we know we found "text-success" or "muted" either once or not at all. If it was found once we return an array of three elements included the first item in the input array, the value of "text-success" or "muted", and then the item that immediately followed that text in the array.

(Note: in your example output from Array 1 you returned the value after the matched text, but in your example output from Array 4 you returned the value before the matched text. I don't know which one you really want, but you can easily modify my function to do one or the other.)

You didn't say what you wanted to return if "text-success" or "muted" wasn't present in the array, so I've just returned a place-holder string.

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

Comments

0

If you want to test if it exists "text-success" or "muted" once in the array, use this function, it will return a boolean:

function singles(array) {
    var single = false;
    for (var i = 0; i < array.length; i++) {
        if (array[i] === "text-success" || array[i] === "muted") {
            if (single) {
                return false;
            }
            single = true;
        }
    }
    return single;
}

If you want to return the unique value among "text-success" or "muted", use the following function. It will return the unique value, or null otherwise:

function singles(array) {
    var single = null;
    for (var i = 0; i < array.length; i++) {
        if (array[i] === "text-success" || array[i] === "muted") {
            if (single) {
                return null;
            }
            single = array[i];
        }
    }
    return single;
}

If you need the code of the corresponding value, use this:

function singles(array) {
    var single = null;
    for (var i = 1; i < array.length; i += 2) {
        if (array[i] === "text-success" || array[i] === "muted") {
            if (single) {
                return null;
            }
            single = array[i - 1];
        }
    }
    return single;
}

4 Comments

Array[11] 0:"265" 1:"text-success" 2:"51"
Do I need to group them
@TanvirAlam I added the code to my answer to get the identifier of the unique value.
@TanvirAlam Oops sorry, replaced i *= 2 by i += 2

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.