JavaScript解析数组并获得唯一值 [英] JavaScript Parsing array and getting the unique value

查看:114
本文介绍了JavaScript解析数组并获得唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

    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"

第一个将返回FALSE,因为两者都同时存在文本成功"两次和静音"一次

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

第二个将返回TRUE,因为它曾经存在文本成功"

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

第三个将返回FALSE,因为两者都存在文本成功"和静音"

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

第四个将返回TRUE,因为它曾经被静音"

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;
};

请帮助任何人?

推荐答案

目前尚不清楚您是否包含一个包含多个数组的对象,或者只是一个标准的一维数组,您已经为其提供了一些可能的示例,但是假设后者,我认为这就是您要寻找的东西:

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"]));

这将循环遍历输入array.第一次找到"text-success""muted"时,其索引存储在foundIndex中.

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

如果第二次找到"text-success""muted",我们将立即返回null.

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

如果我们进入循环的结尾,那么我们知道我们发现一次"text-success""muted"一次还是根本没有.如果找到,则返回一个包含三个元素的数组,其中包括输入数组中的第一项,"text-success""muted"的值,然后是紧跟该数组中文本的项.

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.

(注意:在数组1的示例输出中,在匹配的文本之后返回 ,但在数组4的示例输出中,在返回的之前匹配的文本.我不知道您真正想要的是哪一个,但是您可以轻松地修改我的函数以执行其中一项.

(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.)

您没有说要在数组中不存在"text-success""muted"时要返回的内容,所以我刚刚返回了一个占位符字符串.

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.

这篇关于JavaScript解析数组并获得唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆