Javascript 在jquery中另一个数组的条件下迭代对象列表

Javascript 在jquery中另一个数组的条件下迭代对象列表,javascript,jquery,Javascript,Jquery,我在下面有一张物品清单 newlist也是动态的,SelectID数组也是动态的 我正在填充一个函数,现在我必须迭代并创建新的列表 var newList = [ { id : 1,name="tea",plant:"darjeeling"}, { id : 2,name="coffee",plant:"manipur"}, { id : 3,name="milk",plant:"nilgiri"}, { id : 4,name="tea",

我在下面有一张物品清单

newlist也是动态的,SelectID数组也是动态的

我正在填充一个函数,现在我必须迭代并创建新的列表

  var newList = 
    [
    {  id : 1,name="tea",plant:"darjeeling"},
    {  id : 2,name="coffee",plant:"manipur"},
    {  id : 3,name="milk",plant:"nilgiri"},
    {  id : 4,name="tea",plant:"assam"}
    ]
另一个具有公共ID的数组

var selectedID = [2,3];
现在我必须迭代对象列表,并根据需要更新对象列表 如果ID为2和3,工厂应为“munnar”

那么如何创建新的对象列表“newlist”,如下所示

var newList = 
[
{  id : 1,name="tea",plant:"darjeeling"},
{  id : 2,name="coffee",plant:"munnar"},
{  id : 3,name="milk",plant:"munnar"},
{  id : 4,name="tea",plant:"assam"}
]

您可以迭代想要更改的id,找到对象并更改值

使用:

  • 如果找不到对象,则为默认对象
    {}

var newList=[{id:1,名称:“茶”,植物:“大吉岭”},{id:2,名称:“咖啡”,植物:“曼尼普尔”},{id:3,名称:“牛奶”,植物:“尼尔吉里”},{id:4,名称:“茶”,植物:“阿萨姆”},
选择edid=[2,3];
选择edid.forEach(id=>(newList.find(a=>a.id==id)| |{})。plant='munnar');
console.log(newList)

。作为控制台包装{max height:100%!important;top:0;}
您可以迭代想要更改的id,找到对象并更改值

使用:

  • 如果找不到对象,则为默认对象
    {}

var newList=[{id:1,名称:“茶”,植物:“大吉岭”},{id:2,名称:“咖啡”,植物:“曼尼普尔”},{id:3,名称:“牛奶”,植物:“尼尔吉里”},{id:4,名称:“茶”,植物:“阿萨姆”},
选择edid=[2,3];
选择edid.forEach(id=>(newList.find(a=>a.id==id)| |{})。plant='munnar');
console.log(newList)

.as console wrapper{max height:100%!important;top:0;}
据我所知,您希望基于所选ID的数组使用不同的值操作原始数组。如果从UI中选择的ID与植物数组中的任何ID匹配,那么将名称更改为Munnar?如果我理解正确,那么我相信以下内容对您适用

var newList = [{
  id: 1,
  name: "tea",
  plant: "darjeeling"
}, {
  id: 2,
  name: "coffee",
  plant: "manipur"
}, {
  id: 3,
  name: "milk",
  plant: "nilgiri"
}, {
  id: 4,
  name: "tea",
  plant: "assam"
}]

function munnar(selectedID) {
  newList.forEach( (item) => {
    selectedID.forEach( (id) => {
      if(item.id === id) {
        item.plant = 'Munnar'
      }
    })
  })
}

munnar([2, 3])
这是回报

[{
  id: 1,
  name: "tea",
  plant: "darjeeling"
}, {
  id: 2,
  name: "coffee",
  plant: "Munnar"
}, {
  id: 3,
  name: "milk",
  plant: "Munnar"
}, {
  id: 4,
  name: "tea",
  plant: "assam"
}]

据我所知,您希望基于选定ID的数组,使用不同的值操纵原始数组。如果从UI中选择的ID与植物数组中的任何ID匹配,那么将名称更改为Munnar?如果我理解正确,那么我相信以下内容对您适用

var newList = [{
  id: 1,
  name: "tea",
  plant: "darjeeling"
}, {
  id: 2,
  name: "coffee",
  plant: "manipur"
}, {
  id: 3,
  name: "milk",
  plant: "nilgiri"
}, {
  id: 4,
  name: "tea",
  plant: "assam"
}]

function munnar(selectedID) {
  newList.forEach( (item) => {
    selectedID.forEach( (id) => {
      if(item.id === id) {
        item.plant = 'Munnar'
      }
    })
  })
}

munnar([2, 3])
这是回报

[{
  id: 1,
  name: "tea",
  plant: "darjeeling"
}, {
  id: 2,
  name: "coffee",
  plant: "Munnar"
}, {
  id: 3,
  name: "milk",
  plant: "Munnar"
}, {
  id: 4,
  name: "tea",
  plant: "assam"
}]

我假设您是在javascript中完成这项工作的,所以首先我想指出,您应该像这样定义newList

 var newList = 
    [
    {  id : 1,name:"tea",plant:"darjeeling"},
    {  id : 2,name:"coffee",plant:"manipur"},
    {  id : 3,name:"milk",plant:"nilgiri"},
    {  id : 4,name:"tea",plant:"assam"}
    ]
name=“tea”不起作用

但对于您的问题,您需要使用selectedID数组值来更新现有的newList,如下所示

 var newList = 
    [
    {  id : 1,name:"tea",plant:"darjeeling"},
    {  id : 2,name:"coffee",plant:"manipur"},
    {  id : 3,name:"milk",plant:"nilgiri"},
    {  id : 4,name:"tea",plant:"assam"}
    ]

 var selectedID =[2,3]

for(i=0;i<newList.length;i++){
   for(j=0;j < selectedID.length;j++){
      if(newList[i].id==selectedID[j])
         newList[i].plant = "munnar";
   }
}

console.log(newList);
var newList=
[
{id:1,名称:“茶”,植物:“大吉岭”},
{id:2,名称:“咖啡”,植物:“曼尼普尔”},
{id:3,名称:“牛奶”,植物:“nilgiri”},
{id:4,名称:“茶”,植物:“阿萨姆”}
]
变量selectedID=[2,3]

对于(i=0;i我假设您是在javascript中执行此操作的,所以首先我想指出您应该像这样定义newList

 var newList = 
    [
    {  id : 1,name:"tea",plant:"darjeeling"},
    {  id : 2,name:"coffee",plant:"manipur"},
    {  id : 3,name:"milk",plant:"nilgiri"},
    {  id : 4,name:"tea",plant:"assam"}
    ]
name=“tea”不起作用

但对于您的问题,您需要使用selectedID数组值来更新现有的newList,如下所示

 var newList = 
    [
    {  id : 1,name:"tea",plant:"darjeeling"},
    {  id : 2,name:"coffee",plant:"manipur"},
    {  id : 3,name:"milk",plant:"nilgiri"},
    {  id : 4,name:"tea",plant:"assam"}
    ]

 var selectedID =[2,3]

for(i=0;i<newList.length;i++){
   for(j=0;j < selectedID.length;j++){
      if(newList[i].id==selectedID[j])
         newList[i].plant = "munnar";
   }
}

console.log(newList);
var newList=
[
{id:1,名称:“茶”,植物:“大吉岭”},
{id:2,名称:“咖啡”,植物:“曼尼普尔”},
{id:3,名称:“牛奶”,植物:“nilgiri”},
{id:4,名称:“茶”,植物:“阿萨姆”}
]
变量selectedID=[2,3]


对于(i=0;i和你的特殊问题在哪里?op没有提出任何问题。@Kinduser,重读,没有发现任何问题?op没有提出任何问题。@Kinduser,重读,没有发现任何问题。我不知道为什么它不能在visual studio js中工作file@tripathy,这是ES6风格。但更好的问题是您需要多少数据在
newList
中有多少个索引,在
selectedID
中有多少个索引?内容多久更改一次?索引是否排序?索引不会排序,它们只是来自另一个函数的值。selectedID可以包含许多对象,而newList可以包含许多对象。但是selectedID将包含更少或等于该数字列表中的对象的数目是肯定的不知道为什么它不能在VisualStudioJS中工作file@tripathy,这是ES6风格。但更好的问题是,您在
newList
中有多少数据,在
selectedID
中有多少索引?内容多久更改一次?索引是否已排序?索引不会排序,只是来自另一个函数的值。SeletedID可以包含许多对象,newlist也可以包含许多对象。但是SelectedID将包含少于或等于列表中对象的数量,这是可以确定的,它将基于索引而不是基于
id
值来更改
newlist
。无法更改newlist,只需更改v无论它在哪里找到id,我就在哪里修复它…我不能忍受我做了错事,那会基于索引而不是基于
id
值更改
newList
值。不能更改newList,只要在它找到id的地方更改值,我就在哪里修复它…我不能忍受我做错了