简体   繁体   English

传递的数组的索引

[英]index of array that is passed

How do I retrieve the index of the array that is passed? 如何检索所传递数组的索引? The solution I currently use is sending the index too, but that doesn't feel right. 我当前使用的解决方案也是发送索引,但是感觉不对。

jsFiddle jsFiddle

var obj = {arr: [{x: 1, y: 2},{x: 3, y: 4},{x: 5, y: 6}]};

function myFunction(myObj)
{
    alert(myObj); // 5
    // alert(the index of the array that is passed); // 2
}

myFunction(obj.arr[2].x);

There's no real way to do what you're asking. 没有真正的方法可以完成您要问的事情。 JavaScript is purely call-by-value, which means that before a function call is made the arguments are completely evaluated. JavaScript纯粹是按值调用,这意味着在进行函数调用之前,必须对参数进行完全评估。 All that's left after the evaluation is the final value, and a copy of that is passed to the function. 求值后剩下的就是最终值,并将其副本传递给函数。

You can of course write code that searches for a value in some relatively-global array, but that would be a waste of CPU cycles if you can instead simply pass the array index to the function. 当然,您可以编写代码以在某些相对全局的数组中搜索值,但是如果您可以简单地将数组索引传递给函数,则将浪费CPU周期。

You can use Array.indexOf jsfiddle 您可以使用Array.indexOf jsfiddle

var obj = {arr: [{x: 1, y: 2},{x: 3, y: 4},{x: 5, y: 6}]};

function myFunction(myObj)
{
    alert(obj.arr.indexOf(myObj)); 
}

myFunction(obj.arr[2]);

You can't do that, because the array or the index is not passed to the function, or even the object. 您不能这样做,因为数组或索引没有传递给函数,甚至没有传递给对象。

The array is read from the arr property of the object, then an object is read from the array, then the value is read from the x property of that object, and the function is called with that value. 从对象的arr属性读取数组,然后从数组读取对象,然后从对象的x属性读取值,并使用该值调用函数。

Once inside the function, you can't tell that the value came from a property of an object, or that the object was stored in an array, or that the array was in turn a property in an object. 一旦进入函数,您就无法分辨出该值来自对象的属性,还是该对象存储在数组中,或者该数组又是对象中的属性。

暂无
暂无

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

相关问题 javascript apply不触发传递给它的数组中的每个索引 - javascript apply not firing each index in array passed into it 为什么 javascript 在作为参数或索引传入时将数组解析为字符串 - why does javascript parse an array as a string when passed in as an argument or as an index JS:创建一个方法来返回一个数组,该数组不包含传递给my方法的数组中的索引值 - JS: Create a method to return an array that does not include the index values from the array passed to my method 如何设置自动播放轮播中使用的数组的当前索引的State,以作为prop传递到另一个组件中? - How can I setState of the current index of an array being used in a carousel which autoplays, to be passed into another component as a prop? ReactJS:确定从另一个文件传递的数组中项目的索引。 (使用Ant设计) - ReactJS: Determining the index of an item in an array, that is passed from another file. (Using Ant Design) 成功通过EJS向客户端传递了一个数据数组,但是当我添加索引值时它不会让我在客户端访问该数组 - Successfully passed an array of data to the client via EJS, but it will not let me access the array on the client side when I add an index value 数组作为字符串传递给ajax - Array passed to ajax as a string 更改传递给函数的数组 - change array passed to function 传入函数数组未定义 - Passed into the function array is undefined 未定义传递给数组 - Undefined is passed to array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM