如何使用javascript获取数组中特定id的最后一个值?

时间:2022-11-27 11:29:17

I have a function that store the values of a row in an array onkeyup event. However, there are instances that it would store the same row values but differ on quantity and total but the same id. How would I store it in the array in a way that it would just save the most current set of values of an specific id? I know it's a bit confusing, but please take a look in the image below. Thank you for the help.

我有一个函数存储数组onkeyup事件中的行的值。但是,有些情况下它会存储相同的行值,但数量和总数不同但ID相同。如何以一种只保存特定id的最新值集的方式将它存储在数组中?我知道这有点令人困惑,但请看下面的图片。感谢您的帮助。

如何使用javascript获取数组中特定id的最后一个值?

I would like to save the ff:

我想保存ff:

{id:"1", qty:"4", price:"45", total:"180"}
{id:"2", qty:"3", price:"10", total:"30"}
{id:"3", qty:"50", price:"12", total:"600"}
{id:"4", qty:"60", price:"12", total:"720"}

My Code:

我的代码:

var arrayVar = [];
var data;

$(function(){

  $('#tbl-po-list').on( 'keyup change' , 'input[type="number"]' ,function(){

    $(this).parents('.info').find('.total').val($(this).val() * $(this).parents('.info').find('.price').val());

      data = {
        id: $(this).parents('.info').find('.prod-id').val(),
        qty: $(this).val(),
        price: $(this).parents('.info').find('.price').val(),
        total: $(this).parents('.info').find('.total').val()
      }

      arrayVar.push(data); 

      for(var i = 0; i < arrayVar.length; i++){
        console.log(arrayVar[i]);
      }

    });   

  });

4 个解决方案

#1


2  

You can achieve that if you replace:

如果你替换你可以实现:

arrayVar.push(data); 

by:

通过:

for(var i = 0; i < arrayVar.length; i++){
    if (arrayVar[i].id === data.id) break; // found the same id!
}
arrayVar[i] = data;

If the loop does not find the same id, then after the loop i will equal arrayVar.length, and so the assignment will be like a push.

如果循环找不到相同的id,那么在循环之后i将等于arrayVar.length,因此赋值将类似于push。

If on the other hand the id is found, then the loop exits, and the assignment will replace whatever was in that array element before.

另一方面,如果找到了id,则循环退出,并且赋值将替换之前该数组元素中的任何内容。

A more concise version of the same:

一个更简洁的版本:

for(var i = 0; i < arrayVar.length && arrayVar[i].id !== data.id; i++);
arrayVar[i] = data;

#2


2  

Seems ripe for some code golf. Here's my attempt:

某些代码高尔夫似乎已经成熟。这是我的尝试:

o=data.reduce((a,v)=>a[v.id]=v&&a,{});Object.keys(o).map(k=>o[k]);

Original code:

原始代码:

obj = data.reduce((accum, value, i) => {
  accum[value.id] = value;
  return accum;
})
out = Object.keys(obj).map(key => obj[key]);

This works by using reduce to accumulate values into an object - using the id as the key means rows with the same ID will get overwritten - and then extracts the values from the object.

这通过使用reduce将值累积到对象中来工作 - 使用id作为键意味着具有相同ID的行将被覆盖 - 然后从对象中提取值。

#3


1  

You need to delete the array value using splice() inside of your for loop if it's found:

如果找到,你需要在for循环内使用splice()删除数组值:

var id = $(this).parents('.info').find('.prod-id').val();

for(var i = 0; i < arrayVar.length; i++){
   if(arrayVar[i]['id'] == id){
       arrayVar.splice(i, 1);
   }
}

Example

#4


1  

Iterate from the end to the beginning pushing only new objects (by id) to a new array. unshift keeps the new array in the same order.

从最后到开头迭代,只将新对象(通过id)推送到新数组。 unshift使新数组保持相同的顺序。

var index = [], out = [];
for (var i = arr.length - 1; i >= 0; i--) {
  if (index.indexOf(arr[i].id) === -1) {
    index.push(arr[i].id);
    out.unshift(arr[i]);
  }
}

DEMO

DEMO

#1


2  

You can achieve that if you replace:

如果你替换你可以实现:

arrayVar.push(data); 

by:

通过:

for(var i = 0; i < arrayVar.length; i++){
    if (arrayVar[i].id === data.id) break; // found the same id!
}
arrayVar[i] = data;

If the loop does not find the same id, then after the loop i will equal arrayVar.length, and so the assignment will be like a push.

如果循环找不到相同的id,那么在循环之后i将等于arrayVar.length,因此赋值将类似于push。

If on the other hand the id is found, then the loop exits, and the assignment will replace whatever was in that array element before.

另一方面,如果找到了id,则循环退出,并且赋值将替换之前该数组元素中的任何内容。

A more concise version of the same:

一个更简洁的版本:

for(var i = 0; i < arrayVar.length && arrayVar[i].id !== data.id; i++);
arrayVar[i] = data;

#2


2  

Seems ripe for some code golf. Here's my attempt:

某些代码高尔夫似乎已经成熟。这是我的尝试:

o=data.reduce((a,v)=>a[v.id]=v&&a,{});Object.keys(o).map(k=>o[k]);

Original code:

原始代码:

obj = data.reduce((accum, value, i) => {
  accum[value.id] = value;
  return accum;
})
out = Object.keys(obj).map(key => obj[key]);

This works by using reduce to accumulate values into an object - using the id as the key means rows with the same ID will get overwritten - and then extracts the values from the object.

这通过使用reduce将值累积到对象中来工作 - 使用id作为键意味着具有相同ID的行将被覆盖 - 然后从对象中提取值。

#3


1  

You need to delete the array value using splice() inside of your for loop if it's found:

如果找到,你需要在for循环内使用splice()删除数组值:

var id = $(this).parents('.info').find('.prod-id').val();

for(var i = 0; i < arrayVar.length; i++){
   if(arrayVar[i]['id'] == id){
       arrayVar.splice(i, 1);
   }
}

Example

#4


1  

Iterate from the end to the beginning pushing only new objects (by id) to a new array. unshift keeps the new array in the same order.

从最后到开头迭代,只将新对象(通过id)推送到新数组。 unshift使新数组保持相同的顺序。

var index = [], out = [];
for (var i = arr.length - 1; i >= 0; i--) {
  if (index.indexOf(arr[i].id) === -1) {
    index.push(arr[i].id);
    out.unshift(arr[i]);
  }
}

DEMO

DEMO