具有多个对象的Javascript过滤器数组

时间:2018-05-15 10:58:12

标签: jquery typescript

大家好,我的JSON如下

class Main2Activity : AppCompatActivity() {

private var imgFiles: Array<File?>? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main2)

    imgFiles= arrayOfNulls(2)

    imgFiles!![0] = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/doc1.png")
    imgFiles!![1] = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/doc3.png")



    val file = getOutputFile(File(Environment.getExternalStorageDirectory().absolutePath)
            , "/output.pdf")

    val fOut = FileOutputStream(file)
    val document = PdfDocument()

    var i = 0
    imgFiles?.forEach {
        i++
        val bitmap = BitmapFactory.decodeFile(it?.path)
        val pageInfo = PdfDocument.PageInfo.Builder(bitmap.width, bitmap.height, i).create()
        val page = document.startPage(pageInfo)
        val canvas = page?.canvas
        val paint = Paint()
        canvas?.drawPaint(paint)
        paint.color = Color.BLUE;
        canvas?.drawBitmap(bitmap, 0f, 0f, null)
        document.finishPage(page)
        bitmap.recycle()
    }
    document.writeTo(fOut)
    document.close()        

}

private fun getOutputFile(path: File, fileName: String): File? {
    if (!path.exists()) {
        path.mkdirs()
    }
    val file = File(path, fileName)
    try {
        if (file.exists()) {
            file.delete()
        }
        file.createNewFile()

    } catch (e: Exception) {
        e.printStackTrace()
    }
    return file
}

}

我的过滤器如下

var autoNotifyCategories = {
"notify": [{
    "Category": "Category1",
    "Title": ["Title1", "Title2"],
    "Pending": "true"
  },
  {
    "Category": "Category2",
    "Title": ["Title1"],
    "Pending": "true"
  },
  {
    "Category": "Category3",
    "Title": ["Title1", "Title2", "Title3", "Title4"],
    "Pending": "true"
  },
]

在标题我可以根据要求传递任何内容,但我需要的是我想根据我传递的参数完全过滤并从数组中返回Pending值。我累了如下,但它只检查类别似乎,我需要验证类别和标题可以有人帮助我,这里是他小提琴我试过

&#13;
&#13;
var filter = {
    Category: 'Category3',
    Title: 'Title4'
};
&#13;
// find elements
var banner = $("#banner-message")
var button = $("button")

// handle click and add class
button.on("click", function() {
  //banner.addClass("alt")
  var autoNotifyCategories = {
    "notify": [{
        "Category": "Category1",
        "Title": ["Title1", "Title2"],
        "Pending": "true"
      },
      {
        "Category": "Category2",
        "Title": ["Title1"],
        "Pending": "true"
      },
      {
        "Category": "Category3",
        "Title": ["Title1", "Title2", "Title3", "Title4"],
        "Pending": "true"
      },
    ]
  }
  var filter = {
    Category: 'Category3',
    Title: 'Title4'
  };
  //alert(autoNotifyCategories.notify);
  let s = autoNotifyCategories.notify.filter(function(item) {
    for (var key in filter) {
      if (item[key] === filter[key]) {
        return item.Pending;
      }

    }
  });
  alert(s[0].Pending);
});
&#13;
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#banner-message {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  font-size: 25px;
  text-align: center;
  transition: all 0.2s;
  margin: 0 auto;
  width: 300px;
}

button {
  background: #0084ff;
  border: none;
  border-radius: 5px;
  padding: 8px 14px;
  font-size: 15px;
  color: #fff;
}

#banner-message.alt {
  background: #0084ff;
  color: #fff;
  margin-top: 40px;
  width: 200px;
}

#banner-message.alt button {
  background: #fff;
  color: #000;
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

鉴于Title是一个数组,您可以使用includes进行验证。

var autoNotifyCategories = {
  "notify": [{
      "Category": "Category1",
      "Title": ["Title1", "Title2"],
      "Pending": "true"
    },
    {
      "Category": "Category2",
      "Title": ["Title1"],
      "Pending": "true"
    },
    {
      "Category": "Category3",
      "Title": ["Title1", "Title2", "Title3", "Title4"],
      "Pending": "true"
    },
  ]
}

var filter = {
  Category: 'Category3',
  Title: 'Title4'
};
var filtered = autoNotifyCategories.notify.filter((obj) => {
  return (obj.Category === filter["Category"]) && (obj.Title.includes(filter["Title"]))
})

alert(filtered[0].Pending)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:1)

请检查用JSfiddle编写的代码。数组最初不匹配。 该数组应该被转换为String以进行比较。

现在正在工作..!

    let s = autoNotifyCategories.notify.filter(function(item) {
    let cnt  = 0,equal = 1;
    for (var key in filter) {
      cnt++;
      equal = (item[key].toString().includes(filter[key].toString()));
      if(equal && cnt == Object.keys(filter).length){
            return item[key];
      }
    }
  });

  if(s.length){
    alert(s[0].Pending)
  }else{
    alert('Not Found');
  }

JS小提琴:https://jsfiddle.net/3f2Lmjyj/