Javascript 将数组的项映射到另一个数组
我有一个数组:Javascript 将数组的项映射到另一个数组,javascript,arrays,Javascript,Arrays,我有一个数组: arr = [ 1, 2 , 3 ] 还有另一个数组,我将DOM元素保存为 Elem = [ 1, 3] 我需要迭代arr,并且只在索引匹配的情况下执行操作。例如,当我循环通过arr时,我有元素1和3,所以应该只对1和3发生一些事情,并且应该跳过元素2,因为没有元素2 有人告诉我要研究关联数组,我想知道如何用最少的行数做到这一点 我希望代码简单易读,到目前为止,所有关联数组的示例都毫无意义,而且过于臃肿 for(变量i=0;i-1){ for(var i = 0;i<a
arr = [ 1, 2 , 3 ]
还有另一个数组,我将DOM元素保存为
Elem = [ 1, 3]
我需要迭代arr,并且只在索引匹配的情况下执行操作。例如,当我循环通过arr时,我有元素1和3,所以应该只对1和3发生一些事情,并且应该跳过元素2,因为没有元素2
有人告诉我要研究关联数组,我想知道如何用最少的行数做到这一点
我希望代码简单易读,到目前为止,所有关联数组的示例都毫无意义,而且过于臃肿 for(变量i=0;i-1){
for(var i = 0;i<arr.length;i++){
if(Elem.indexOf(arr[i])>-1){
//Elem contains arr[i] (contains object that at index i in arr)
//will be called only for 1 and 3 in arr
arr[i] = ... //do what you want with this object.
}
}
//元素包含arr[i](包含在arr中索引i处的对象)
//将仅在arr中为1和3调用
arr[i]=…//对该对象执行所需操作。
}
}
你是这个意思吗?用于(VARI=0;i-1){
//元素包含arr[i](包含在arr中索引i处的对象)
//将仅在arr中为1和3调用
arr[i]=…//对该对象执行所需操作。
}
}
你是这个意思吗?用于(VARI=0;i-1){
//元素包含arr[i](包含在arr中索引i处的对象)
//将仅在arr中为1和3调用
arr[i]=…//对该对象执行所需操作。
}
}
你是这个意思吗?用于(VARI=0;i-1){
//元素包含arr[i](包含在arr中索引i处的对象)
//将仅在arr中为1和3调用
arr[i]=…//对该对象执行所需操作。
}
}
你是说这个吗?我稍微修改了第二个数组,允许在一个地方定义多个操作。我不确定我是否正确地理解了你
// array of DOM objects available
var arr = ['object1-selector', 'object2-selector', 'object3-selector'];
// array of actions with items that the method should be applied to
var actions = [
{
items: ['object1-selector', 'object3-selector'],
perform: function(elem) {
alert(elem);
}
},
{
items: ['object2-selector'],
perform: function(elem) {
alert(elem);
}
},
{
items: ['object4-selector'],
perform: function(elem) {
alert(elem);
}
}
];
//forEach loop that iterates over actions and checks if selector exists.
//If yes - it invokes the method
actions.forEach(function(action) {
action.items.forEach(function(item) {
if(arr.indexOf(item) > -1) {
action.perform(item);
}
});
});
如果您想在一个地方定义动作,在多维数组中定义对象,请告诉我。我将尝试调整这个例子。如果不存储选择器,而是存储整个DOM对象,只需修改项:
数组和循环,检查元素是否存在
哦,这里是JSFIDLE:。jQuery仅用于alert()以向您展示工作示例。我稍微修改了第二个数组,以允许在一个位置定义多个操作。我不确定我是否正确地理解了你
// array of DOM objects available
var arr = ['object1-selector', 'object2-selector', 'object3-selector'];
// array of actions with items that the method should be applied to
var actions = [
{
items: ['object1-selector', 'object3-selector'],
perform: function(elem) {
alert(elem);
}
},
{
items: ['object2-selector'],
perform: function(elem) {
alert(elem);
}
},
{
items: ['object4-selector'],
perform: function(elem) {
alert(elem);
}
}
];
//forEach loop that iterates over actions and checks if selector exists.
//If yes - it invokes the method
actions.forEach(function(action) {
action.items.forEach(function(item) {
if(arr.indexOf(item) > -1) {
action.perform(item);
}
});
});
如果您想在一个地方定义动作,在多维数组中定义对象,请告诉我。我将尝试调整这个例子。如果不存储选择器,而是存储整个DOM对象,只需修改项:
数组和循环,检查元素是否存在
哦,这里是JSFIDLE:。jQuery仅用于alert()以向您展示工作示例。我稍微修改了第二个数组,以允许在一个位置定义多个操作。我不确定我是否正确地理解了你
// array of DOM objects available
var arr = ['object1-selector', 'object2-selector', 'object3-selector'];
// array of actions with items that the method should be applied to
var actions = [
{
items: ['object1-selector', 'object3-selector'],
perform: function(elem) {
alert(elem);
}
},
{
items: ['object2-selector'],
perform: function(elem) {
alert(elem);
}
},
{
items: ['object4-selector'],
perform: function(elem) {
alert(elem);
}
}
];
//forEach loop that iterates over actions and checks if selector exists.
//If yes - it invokes the method
actions.forEach(function(action) {
action.items.forEach(function(item) {
if(arr.indexOf(item) > -1) {
action.perform(item);
}
});
});
如果您想在一个地方定义动作,在多维数组中定义对象,请告诉我。我将尝试调整这个例子。如果不存储选择器,而是存储整个DOM对象,只需修改项:
数组和循环,检查元素是否存在
哦,这里是JSFIDLE:。jQuery仅用于alert()以向您展示工作示例。我稍微修改了第二个数组,以允许在一个位置定义多个操作。我不确定我是否正确地理解了你
// array of DOM objects available
var arr = ['object1-selector', 'object2-selector', 'object3-selector'];
// array of actions with items that the method should be applied to
var actions = [
{
items: ['object1-selector', 'object3-selector'],
perform: function(elem) {
alert(elem);
}
},
{
items: ['object2-selector'],
perform: function(elem) {
alert(elem);
}
},
{
items: ['object4-selector'],
perform: function(elem) {
alert(elem);
}
}
];
//forEach loop that iterates over actions and checks if selector exists.
//If yes - it invokes the method
actions.forEach(function(action) {
action.items.forEach(function(item) {
if(arr.indexOf(item) > -1) {
action.perform(item);
}
});
});
如果您想在一个地方定义动作,在多维数组中定义对象,请告诉我。我将尝试调整这个例子。如果不存储选择器,而是存储整个DOM对象,只需修改项:
数组和循环,检查元素是否存在
哦,这里是JSFIDLE:。jQuery仅用于alert()以向您展示工作示例。不确定如何识别第二个数组中的元素,但这是我的建议。带ID的数组
var arr = [ "id_1", "id_2", "id_3" ]
var Elem = {
"id_1": html_element,
"id_2": html_element,
"id_3": html_element
}
那么你需要做的就是
for( var i = 0; i < arr.length; i++ ) {
if( Elem[ arr[i] ] ) {
// do stuff
}
}
for(变量i=0;i
不确定如何识别第二个数组中的元素,但这是我的建议。带ID的数组
var arr = [ "id_1", "id_2", "id_3" ]
var Elem = {
"id_1": html_element,
"id_2": html_element,
"id_3": html_element
}
那么你需要做的就是
for( var i = 0; i < arr.length; i++ ) {
if( Elem[ arr[i] ] ) {
// do stuff
}
}
for(变量i=0;i
不确定如何识别第二个数组中的元素,但这是我的建议。带ID的数组
var arr = [ "id_1", "id_2", "id_3" ]
var Elem = {
"id_1": html_element,
"id_2": html_element,
"id_3": html_element
}
那么你需要做的就是
for( var i = 0; i < arr.length; i++ ) {
if( Elem[ arr[i] ] ) {
// do stuff
}
}
for(变量i=0;i
不确定如何识别第二个数组中的元素,但这是我的建议。带ID的数组
var arr = [ "id_1", "id_2", "id_3" ]
var Elem = {
"id_1": html_element,
"id_2": html_element,
"id_3": html_element
}
那么你需要做的就是
for( var i = 0; i < arr.length; i++ ) {
if( Elem[ arr[i] ] ) {
// do stuff
}
}
for(变量i=0;i
所以第二个数组中有DOM元素,但这些数字是从哪里来的?“到目前为止,所有关联数组的示例都没有意义,而且过于臃肿”首先,JavaScript没有关联数组。它有对象,有时也称为地图或字典。这里有一个非膨胀的例子:{foo:bar}
。它使用名为foo
的属性和名为“bar”
的值定义对象。您也可以使用字符串而不是文字和括号表示法来实现这一点:var obj={};var s=“foo”;obj[s]=“bar”
创建相同的对象。所以在第二个数组中有DOM元素,但这些数字是从哪里来的?“到目前为止,所有关联数组的示例都没有意义,而且过于臃肿”首先,JavaScript没有关联数组。它有对象,有时也称为地图或字典。这里有一个非膨胀的例子:{foo:bar}
。它使用名为foo
的属性和名为“bar”
的值定义对象。您也可以使用字符串而不是文字和括号表示法来实现这一点:var obj={};var s=“foo”;obj[s]=“bar”
创建相同的对象。所以在第二个数组中有DOM元素,但这些数字是从哪里来的?“到目前为止,所有关联数组的示例都没有意义,而且过于臃肿”首先,JavaScript没有关联数组。它有obj