简体   繁体   English

从属性等于值的数组中获取项目

[英]Get Item from Array with Property equal to Value

This questions is for my LightSwitch project but I'm not sure that's renavent.这个问题是针对我的 LightSwitch 项目的,但我不确定那是 renaven。 I've never done javascript or jquery before and i'm pretty sure this will have nothing to do with LightSwitch and everything to do with the latter.我以前从未使用过 javascript 或 jquery,我很确定这与 LightSwitch 无关,而与后者有关。

The following code works perfectly setting test equal to 4 in my case:在我的情况下,以下代码可以完美地将 test 设置为 4 :

myapp.BrowseTreeNodes.TreeNodes_render = function (element, contentItem) {
    var screen = contentItem.screen;
    var result = screen.MyArray.data[0];
    var test = result.Id;
}

What I need to do is instead of setting result to the first item in the array I need to set result to the item with a specific Id, for this example let's say 4.我需要做的是不是将结果设置为数组中的第一个项目,而是将结果设置为具有特定 Id 的项目,在此示例中,假设为 4。

Here's what i've tried:这是我尝试过的:

var result = $.grep(screen.MyArray.data, function (e) { return e.Id === 4; })[0];
var result = screen.MyArray.data.filter(function (v) { return v.Id === 4; })[0];
var result = screen.MyArray.data.find(x => x.Id === 4)[0];

Thank you in advance.先感谢您。

您的问题可能是=== , ===检查类型,因此"4" === 4false"4" == 4true ,请检查您的 id 属性是否存储为字符串或整数...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM