javascript - 如何在循环函数中过滤已过滤的id来过滤另一个

标签 javascript json loops

console.log 为:A0004A0003A0001A0005A0004A0002A0001

但我预计 console.log 将为:A0004,A0001

因此,这个循环函数中缺少什么?

var imagedata = [{
    id: "A0007",
    class: "chair house grass"
  },
  {
    id: "A0006",
    class: "car house tree flower grass"
  },
  {
    id: "A0005",
    class: "car house tree flower"
  },
  {
    id: "A0004",
    class: "car tree"
  },
  {
    id: "A0003",
    class: "chair tree flower grass"
  },
  {
    id: "A0002",
    class: "chair house flower"
  },
  {
    id: "A0001",
    class: "car flower"
  },
  {
    id: "A0000",
    class: "chair car house tree flower grass"
  }
];

function myFunction() {


  var o2y = ["1", "all", "0", "1", "all", "0"];
  var o3x = ["chair", "car", "house", "tree", "flower", "grass"];
  var m0x = "";
  var m1x = "";
  for (i = 0; i < o3x.length; i++) {
    m0x = o3x[i];
    for (j = 0; j < imagedata.length; j++) {
      if (o2y[i] == "0") {
        if (imagedata[j].class.search(m0x) < 0) {
          m1x += imagedata[j].id;
          console.log(m1x);
        }
      }
    };
  };
}
myFunction();

最佳答案

尝试下面的代码,

var imagedata = [{
      id: "A0007",
      class: "chair house grass"
    },
    {
      id: "A0006",
      class: "car house tree flower grass"
    },
    {
      id: "A0005",
      class: "car house tree flower"
    },
    {
      id: "A0004",
      class: "car tree"
    },
    {
      id: "A0003",
      class: "chair tree flower grass"
    },
    {
      id: "A0002",
      class: "chair house flower"
    },
    {
      id: "A0001",
      class: "car flower"
    },
    {
      id: "A0000",
      class: "chair car house tree flower grass"
    }
    ];

    function myFunction() {


      var o2y = ["1", "all", "0", "1", "all", "0"];
      var o3x = ["chair", "car", "house", "tree", "flower", "grass"];
      var m0x = "";
      var m1x = "";
      var index = [];

      for (i = 0; i < o2y.length; i++) 
        if (o2y[i] == "0") 
          index.push(i);

        for (j = 0; j < imagedata.length; j++) {
          var flag = 0
          for (k = 0; k < index.length; k++) 
            if (imagedata[j].class.search(o3x[index[k]]) < 0) 
              flag++
              
          if(flag == index.length)
            m1x += imagedata[j].id;

        }
        console.log(m1x);
      }
      myFunction();

关于javascript - 如何在循环函数中过滤已过滤的id来过滤另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52343106/

相关文章:

c++ - 为什么选择 for (;;){} 而不是 while(1)?

javascript - tiptap prosemirror 应该仅可编辑特定节点

javascript - 用我的 json 数据填充 JQuery DataTable

javascript - 如何在 javascript 中存储对变量的引用?

ios - 在 UITableView 中使用来自网络调用的 Json 数据

jquery - 如何以json格式发送数据库查询结果并在javascript中显示结果?

objective-c - 遍历数组并 append 到 Objective-C 中的字符串

javascript - 如何检查值是否是数字

javascript - 如何将文件附加到formdata angular 2

javascript - 关于 JavaScript new Date() 和 Date.parse()