如何按属性过滤对象数组?

时间:2016-06-09 22:18:07

标签: javascript d3.js

给定以下数组,其中包含对象数组:

[
  [
    {color: blue, size: 3},
    {color: red, size: 1},
    {color: blue, size: 4}
  ],
  [
    {color: blue, size: 4},
    {color: green, size: 9},
    {color: gren, size: 3}
  ]
]

我如何过滤数据,以便我只留下具有蓝色属性的对象,如下所示:

[
  [
    {color: blue, size: 3},
    {color: blue, size: 4}
  ],
  [
    {color: blue, size: 4}
  ]
]

这是在D3js的上下文中,但这可能只是一个简单的JavaScript问题。

2 个答案:

答案 0 :(得分:2)

这是一种方式:

class Program
{
    #region Cosmos Builder logic
    // Most users wont touch this. This will call the Cosmos Build tool
    [STAThread]
    static void Main(string[] args)
    {
        BuildUI.Run();
    }
    #endregion
    public static string[] outputline = { "Welcome user" };

    // Main entry point of the kernel
    public static void Init()
    {
        var xBoot = new Cosmos.Sys.Boot();
        xBoot.Execute();
        Console.WriteLine("Welcome to Tammaa OS ");
        Console.WriteLine("Please insert a Command");
        Drawconsole();
        string uinput = "";
        uinput = Console.ReadLine();
        if (uinput == "shutdown")
        {
            Cosmos.Sys.Deboot.ShutDown();
        }
        else if (uinput == "help")
        {
            Console.WriteLine("shutdown \t Turn off the system");
            Console.WriteLine("restart \t Restarts the system");
        }
        else if (uinput == "calc")
        {
            Console.WriteLine("Please enter the the syntax");
            String operation = Console.ReadLine();
            uint usernum1, usernum2;
           Console.WriteLine("Please insert the 2 numbers");
           usernum1 = userint(0, 100, 101);
           usernum2 = userint(0, 100, 101);
           if (operation == "+")
           {
               Console.WriteLine((usernum1 + usernum2).ToString());
               Drawconsole();
           }
           else if (operation == "-")
           {
               Console.WriteLine((usernum1 - usernum2).ToString());
               Drawconsole();
           }
           else if (operation == "*")
           {
               Console.WriteLine((usernum1 * usernum2).ToString());
               Drawconsole();
           }
           else if (operation == "/")
           {
               Console.WriteLine((usernum1 / usernum2).ToString());
               Drawconsole();
           }
           else
           {
               Console.WriteLine("An unexcepected error  stoped the proccess please chack your data and try again");
               Drawconsole();
           }
        }
        else
        {
            Console.WriteLine("Unknown Command");
            Console.WriteLine("Type help to get vaild commands,Please");
            Drawconsole();
        }

        while (true)
            ;

    }
    public static uint userint(uint start, uint end, uint Default)
    {
        uint a;
        String userinput = Console.ReadLine();
        for (a = start; a <= end; a++)
        {
            if (a.ToString() == userinput)
            {
                return a;
            }
        }
        return Default;
    }
    public static void Drawconsole()
    {
        Console.Clear();
        Console.WriteLine("OS command here>");

    }
}

outputArray = inputArray.map(function(a) { return a.filter(function(el) { return el.color === "blue"; }); }); 方法创建一个新数组,其元素将是调用您为原始数组中的每个元素提供的函数的结果。 (如果要覆盖原始数组,只需将结果分配回同一个变量而不是新变量。)

.map()方法创建一个新数组,其中只包含原始元素,这些元素在您传递的函数中通过测试。

进一步阅读:

答案 1 :(得分:0)

您可以使用带有循环的蓝色的if-else语句遍历数组。

相关问题