javascript 将函数结果存储到变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15117874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:34:08  来源:igfitidea点击:

Store function result to variable

javascriptvariablesobject

提问by captainrad

How can I store the result of a function to a variable?

如何将函数的结果存储到变量中?

In navigating an object that contains an array, I am looking for one value, value1, once that is found I want to get the value of one of its properties, property1.

在导航包含数组的对象时,我正在寻找一个值value1,一旦找到,我想获取其属性之一的值property1

The code I am using below is an example and is incorrect.

我在下面使用的代码是一个例子,是不正确的。

function loadSets(id){
   clworks.containers.find(function(i){
      return i.get('property1')===id;
   });
}

My intent is to navigate the object below:

我的目的是导航下面的对象:

clworks.containers
    containers[0].property0.id
    containers[1].property1.id

I am trying to determine how to find which item in the array has a property value equal to the id used in the function and then store it as a variable.

我试图确定如何找到数组中哪个项目的属性值等于函数中使用的 id,然后将其存储为变量。

回答by Sunyatasattva

Simply:

简单地:

var myVar = loadSets(id);

EDIT

编辑

Ok, as much as I understand your question now, your situation is the following:

好的,据我现在了解您的问题,您的情况如下:

  1. You have an arraycontaining objects called containers;
  2. You want to iterate through this array, looking for the property idof the property property1which equals the one specified in the function called loadSets(id);
  3. Once found, store the object with the requested idin a variable.
  1. 您有一个array名为containers;的包含对象。
  2. 你想遍历这个array,寻找与被调用函数中指定id的属性property1相等的属性loadSets(id)
  3. 找到后,将具有请求的对象存储id在变量中。

Am I right? If so, this should solve your problem:

我对吗?如果是这样,这应该可以解决您的问题:

// This function iterates through your array and returns the object
// with the property id of property1 matching the argument id
function loadSets( id ) {

    for(i=0; i < containers.length; i++) {

        if( containers[i].property1.id === id )
            return containers[i];

    }

    return false;

}

After this you just need to do what I said in the first answer to your question, triggering it however you want. I put up a quick JSBin for you. Try to put 10, or 20, in the inputfield and then hitting the findbutton; it will return the object you are looking for. Try putting any other number, it will return a Not found.

在此之后,您只需要按照我在您的问题的第一个答案中所说的去做,根据需要触发它。我为你提供了一个快速的 JSBin。尝试在input字段中放置 10 或 20,然后点击查找按钮;它将返回您正在寻找的对象。尝试输入任何其他数字,它会返回一个Not found

回答by tjk

Currently your function, loadSets, isn't actually returning anything and so you cannot store a result from it.

目前,您的函数 loadSets 实际上并未返回任何内容,因此您无法从中存储结果。

Try:

尝试:

function loadSets(id){
   return Base.containers.find(function(i){
      return i.get('entityid')===id;
   });
}

And to get a result into a variable:

并将结果转化为变量:

var result = loadSets(id);