从对象数组中查找有效对象

时间:2017-09-27 14:33:44

标签: javascript

我的javascript代码中有以下数组:

var docHandlerType = Java.type("org.xml.sax.HandlerBase");
var docHandler = new docHandlerType();
docHandler.characters("bla".toCharArray(), 0, 3);
print("Successfully called DocumentHandler.characters");

从这个系列我想通过电子邮件检索用户。例如,我会写:

const users = [ { id: 1, email: 'test@user.com', password: 'password', access_token: 'test_user_access_token' }, { id: 2, email: 'second@user.com', password: 'password', access_token: 'second_user_access_token' } ] 它将返回这一个用户。我怎么能这样做?

3 个答案:

答案 0 :(得分:3)

您可以使用Array#find功能。将谓词传递给函数,该函数将根据该谓词返回第一个匹配项。

            Xrm.Utility.alertDialog("about to check state");
            if (getFieldValue("hc_stateid") == null && result["_hc_state_value"] != null) {// checling if value is null on tni becacuse the else if wouldnt execute b/c no id
                Xrm.Utility.alertDialog("its null");
                Xrm.Page.getAttribute("hc_stateid").
                  setValue([
                      {
                          id: result["_hc_state_value"],
                          name: result["_hc_state_value@OData.Community.Display.V1.FormattedValue"],
                          entityType: result["_hc_state_value@Microsoft.Dynamics.CRM.lookuplogicalname"]
                      }]);
                Xrm.Utility.alertDialog("state changes");
            }

            else if (result["_hc_state_value"] != getFieldValue("hc_stateid")[0].id.replace("{", "").replace("}", "").toString().toLowerCase()) {//tni has a value


                if (result["_hc_state_value"] == null) {
                    Xrm.Page.getAttribute("hc_stateid").setValue(null);
                }
                else {


                    Xrm.Page.getAttribute("hc_stateid").
                        setValue([
                            {
                                id: result["_hc_state_value"],
                                name: result["_hc_state_value@OData.Community.Display.V1.FormattedValue"],
                                entityType: result["_hc_state_value@Microsoft.Dynamics.CRM.lookuplogicalname"]
                            }]);
                    Xrm.Utility.alertDialog("state changes");
                }
            }


            Xrm.Utility.alertDialog("about to check country");
            /*if (getFieldValue("hc_countryid") == null && result["_hc_country_value"] != null)
            {
                Xrm.Page.getAttribute("hc_countryid").
                        setValue([
                            {
                                id: result["_hc_country_value"],
                                name: result["_hc_country_value@OData.Community.Display.V1.FormattedValue"],
                                entityType: result["_hc_country_value@Microsoft.Dynamics.CRM.lookuplogicalname"]
                            }]);
                Xrm.Utility.alertDialog("country changed");

            }
            else if (result["_hc_country_value"] != getFieldValue("hc_countryid")[0].id.replace("{", "").replace("}", "").toString().toLowerCase()) {
                if (result["_hc_country_value"] == null) {
                    Xrm.Page.getAttribute("hc_countryid").setValue(null);
                }
                else {
                    Xrm.Page.getAttribute("hc_countryid").
                        setValue([
                            {
                                id: result["_hc_country_value"],
                                name: result["_hc_country_value@OData.Community.Display.V1.FormattedValue"],
                                entityType: result["_hc_country_value@Microsoft.Dynamics.CRM.lookuplogicalname"]
                            }]);
                    Xrm.Utility.alertDialog("country changed");
                }
            } */

            Xrm.Utility.alertDialog("about to check prim spec");
            Xrm.Utility.alertDialog("prim specialty: " + (result["_hc_primaryspecialty_value"] != getFieldValue("hc_primaryspecialtyid")[0].id.replace("{", "").replace("}", "").toString().toLowerCase()).toString());
            if (getFieldValue("hc_primaryspecialtyid") == null && result["_hc_primaryspecialty_value"] != null) {
                Xrm.Page.getAttribute("hc_primaryspecialtyid").
                      setValue([
                          {
                              id: result["_hc_primaryspecialty_value"],
                              name: result["_hc_primaryspecialty_value@OData.Community.Display.V1.FormattedValue"],
                              entityType: result["_hc_primaryspecialty_value@Microsoft.Dynamics.CRM.lookuplogicalname"]
                          }]);
            }

            else if (result["_hc_primaryspecialty_value"] != getFieldValue("hc_primaryspecialtyid")[0].id.replace("{", "").replace("}", "").toString().toLowerCase()) {
                Xrm.Utility.alertDialog("inside else if");
                if (result["_hc_primaryspecialty_value"] == null) {
                    Xrm.Page.getAttribute("hc_primaryspecialtyid").setValue(null);
                }
                else {
                    Xrm.Utility.alertDialog("prime spec");
                    Xrm.Page.getAttribute("hc_primaryspecialtyid").
                        setValue([
                            {
                                id: result["_hc_primaryspecialty_value"],
                                name: result["_hc_primaryspecialty_value@OData.Community.Display.V1.FormattedValue"],
                                entityType: result["_hc_primaryspecialty_value@Microsoft.Dynamics.CRM.lookuplogicalname"]
                            }]);
                }
            }
            else {

            }

            Xrm.Utility.alertDialog("after prim spec");

答案 1 :(得分:2)

这就是.find()方法的用途。

$duplicates = Get-Content $file | Group -NoElement | Where {$_.count -gt 1} |Select-Object -ExpandProperty Name

因此在数组上调用const users = [ { id: 1, email: 'test@user.com', password: 'password', access_token: 'test_user_access_token' }, { id: 2, email: 'second@user.com', password: 'password', access_token: 'second_user_access_token' } ]; console.log(users.find(u => u.email == 'test@user.com'));,并接收回调函数。将为数组中的每个项调用回调,为此返回将.find()属性与您要查找的电子邮件进行比较的结果。

一旦你的回调返回.email(或truthy)结果,迭代就会停止,并从true返回该对象。如果找不到,.find()会返回.find()

请注意,这使用箭头函数语法。如果您愿意,可以使用传统功能。

undefined

答案 2 :(得分:1)

总有很好的老式for-loop:

<script>
  function getChildrenRecursively(parent) {
    let returnValue = [];
    let children = getChildren(parent);
    children.forEach(function(child, index, array) {
      let itemData = new Object();
      itemData.id = parent + "_" + index + "_" + child;
      itemData.items = getChildrenRecursively(child);

      returnValue.push(itemData);
    })
    return returnValue;
  }

  function getChildren(parentId) {
    if (parentId == 1) return [2, 3];
    if (parentId == 2) return [4];
    if (parentId == 3) return [5, 6];
    if (parentId == 4) return [];
    if (parentId == 5) return [];
    if (parentId == 6) return [];
  }

  console.log(getChildrenRecursively(1));
</script>