简体   繁体   English

修改数组$(this)的元素

[英]Modifying an element of array $(this)

I'm using a 3rd party javascript library which requires a nested array to be passed to a function. 我使用的是第三方JavaScript库,该库需要将嵌套数组传递给函数。 I'm using this function several times, and so storing the array in a variable and passing it to the functions. 我多次使用此函数,因此将数组存储在变量中并将其传递给函数。 However, part of the string of 1 element of the array needs to be the current id of the thing I'm passing; 但是,数组的1个元素的字符串的一部分必须是我要传递的事物的当前ID; I've got something that works that looks a bit like: 我有一些看起来像这样的东西:

myArray = [["foo bar id","some data"],["foo2 bar2 id","some other data"]]

func1(($m1 = myArray.slice(0), 
       $.each($m1, function(i){
              $m1[0] = myArray[i][0].replace("id","1");
       })))

Is it really necessary to make a copy of the array; 是否真的需要复制数组? is there a way of doing it via the $(this) object, or would that not work because that's only in the scope of the .each loop, and doesn't get passed back to func1 ? 是否有一种通过$(this)对象完成$(this) ,或者由于仅在.each循环的范围内并且不会传递回func1而无法正常工作吗?

Update 更新

The original answer below doesn't seem to work - the values of the array the 2nd time I reference it are the values given in the first instance: 下面的原始答案似乎不起作用-我第二次引用它时,数组的值是第一个实例中给出的值:

myArray = [["foo bar id","some data"],["foo2 bar2 id","some other data"]]
func1(

  myArray = myArray.map(function(value)
  {
    value[0] = value[0].replace(/\bid\b/gi, '1');
    console.log(value[0]); //returns "foo bar 1" as expected
    return value;
  });

)

func1(

  myArray = myArray.map(function(value)
  {
    value[0] = value[0].replace(/\bid\b/gi, '2');
    console.log(value[0]); //This returns "foo bar 1", not "foo bar 2"!
    return value;
  });

)

Update (full proper code!) 更新(完整的正确代码!)

To put it all into context, the 3rd party library is jsmol , which allows molecular graphics to be displayed. 为了将它们放到上下文中,第三方库是jsmol ,它允许显示分子图形。 jsmol includes some convenience functions for simple html elements, the one I'm using is Jmol.jmolMenu() - the nested array I've actually got is jsmol包含一些用于简单html元素的便捷函数,我正在使用的一个是Jmol.jmolMenu() -我实际上得到的嵌套数组是

arr = [ ["select model=id; color blue", "colour this model blue"],
        ["select model=id; color green", "colour this model green"]
      ]

and then I have n molecular models displayed in the same applet , which jsmol references as 1.1, 2.1, 3.1, ... etc 然后我在同一applet显示了n个分子模型,jsmol引用为1.1、2.1、3.1等

I have n Jmol.jmolMenu s (which just create html select lists); 我有n Jmol.jmolMenu s(只创建html选择列表); the 1st one to change the colour of 1.1, the second one to change the colour of 2.1 and so on - this is where I need to change the value of the id to the relevant model, and why the replace can only replace the current instance of the current copy - otherwise it makes the lists for the other molecules not work! 第一个更改1.1的颜色,第二个更改2.1的颜色,依此类推-在这里我需要将id的值更改为相关模型,以及为什么replace只能替换当前实例当前副本的副本-否则会使其他分子的列表不起作用!

In my first menu, I effectively need 在我的第一个菜单中,我实际上需要

Jmol.jmolMenu(jmol,
    [
      ["select model=1.1; color blue", "colour this model blue"],
      ["select model=1.1; color green", "colour this model green"]
    ]
);

and in my second: 在第二个:

Jmol.jmolMenu(jmol,
    [
      ["select model=2.1; color blue", "colour this model blue"],
      ["select model=2.1; color green", "colour this model green"]
     ]
);

I hope this makes a bit of sense, and explains why I need the arrays modifying when I do (which might have seemed weird before!) 我希望这很有道理,并解释了为什么我需要在进行修改时修改数组(以前可能看起来很奇怪!)

Update: 更新:
Seeing as you're dealing with a nested array, my initial answer doesn't quite suffice, but still, it's an easy fix: 看到您正在处理嵌套数组时,我的初始答案还不够,但是仍然可以轻松解决:

var myArray = ["foo bar id","my first array"];
myArray = myArray.map(function(value)
{
    return value.replace('id','1');//replaces only the first occurrence of "id", thouh
});

Why are you using jQuery to alter a standard JS array? 为什么要使用jQuery更改标准JS数组? Why not use plain vanillaJS? 为什么不使用普通的vanillaJS? It's faster, shorter and a lot easier to write here: 在这里编写起来更快,更短,更容易:

var myArray = [["foo bar id","some data"],["foo2 bar2 id","some other data"]];
myArray = myArray.map(function(value)
{
    value[0] = value[0].replace(/\bid\b/gi, '1');
    return value;
});

Just map, as before, but treat the value argument as the array that it is. 像以前一样映射,但是将value参数视为它是数组。 If you need the keys of each sub-array, for example to access the corresponding id in another array: 如果您需要每个子数组的键,例如访问另一个数组中的相应ID:

var ids = ['1', '2'];
myArray = myArray.map(function(value, idx)//second argument is the key
{
    value[0] = value[0].replace(/\bid\b/gi, ids[idx]);
    return value;
});

That's all there is to it. 这里的所有都是它的。

There is a "gotcha" you might encounter with your current code. 您当前的代码可能会遇到一个“陷阱” As I mentioned in the comment, your replace call will only replace the first occurrence of "id" in the string, not all of them. 正如我在评论中提到的那样,您的replace调用将仅替换字符串中第一次出现的“ id” ,而不是全部替换。 It'll also replace "id", even if it is part of a word, and the replace call is case-sensitive: 即使它是单词的一部分,它也将替换“ id”,并且replace调用区分大小写:

'ID placid id'.replace('id', '1');//ID plac1 id

These issues can be solved by using a regex: 这些问题可以通过使用正则表达式解决:

'ID placid id'.replace(/\bid\b/gi, '1');//1 placid 1

the \\b in the pattern stands for a word-boundary . 模式中的\\b表示单词边界 the g flag makes the pattern global , all occurrences of id will be replaced. g标志使模式成为全局模式,所有出现的id将被替换。 the i flag makes the pattern case-insensitive. i标志使模式不区分大小写。

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

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