如何在JavaScript中使用相同的属性ID时更新同一数组对象内的属性值。

huangapple go评论75阅读模式
英文:

How to update the property value within same array object when one of property id same using javascript

问题

对于数组对象arrobj,传递一个名为paramid的参数。

如果arrobj属性id的值与paramid相同,则将属性price更新为null,并且对象的oldValue(指的是设置为null之前的价格值)应该更新为仅对于oldvalue大于price的价格。以如下格式更新:

像oldvalue,oldvalue+1,oldvalue+2... 如何使用JavaScript实现这一点。

示例 1

let paramid = 25
const arrobj = [
  { id: 10, price: null },
  { id: 12, price: 1 },
  { id: 14, price: 2 },
  { id: 25, price: 3 }, 
  { id: 23, price: 4 },
  { id: 43, price: 5 },
];
// arrobj的id与paramid相同,`price`变为`null`,`id`为25的对象的oldvalue价格`3`移至下一个对象id`23`(oldvalue),然后移至id`43`的价格(oldvalue+1)
预期输出
 [
  { id: 10, price: null },
  { id: 12, price: 1 },
  { id: 14, price: 2 },
  { id: 25, price: null }, 
  { id: 23, price: 3 },
  { id: 43, price: 4 },
];

示例 2
let paramid = 43
const arrobj = [
  { id: 10, price: null },
  { id: 13, price: 1 },
  { id: 14, price: 2 },
  { id: 25, price: 5 }, 
  { id: 23, price: 6 },
  { id: 43, price: 4 },
  { id: 28, price: 7 }
];

// arrobj的id与paramid相同,`price`变为`null`,`id`为43的对象的oldvalue价格`4`移至下一个对象id`25`,然后移至id`23`	

预期输出
 [
  { id: 10, price: null },
  { id: 12, price: 1 },
  { id: 14, price: 2 },
  { id: 25, price: 4 }, 
  { id: 23, price: 5 },
  { id: 43, price: null },
  { id: 28, price: 6 }
];

注意:价格始终更新为下一个连续的价格值。

已尝试的代码如下:

const getSameId = arrobj(e => e.id === paramid);

let oldValue = getSameId.price;

for (let item of arrobj) {
  const index = arrobj.index(item)
  if (item.id === paramid) {
    ...item,
    price: null 
  }
  if (item.id !== paramid && oldValue > item.price) {
    ...item,
    price: oldValue + 1 
  }
}

请注意,上述代码片段中的...item并不是有效的JavaScript语法。您需要以正确的方式更新数组中的对象。如果需要进一步的帮助,请提出具体的问题。

英文:

For the array of objects arrobj , passing a parameter called paramid.

if the arrobj property

id has same value as paramid, then update the property price to null

and oldValue (refers to price value before set to null) of object should be updated to

remaining objects only for oldvalue is greater than price price.

Update in the format

like oldvalue, oldvalue+1, oldvalue+2... how to achieve this using javascript.

example 1
let paramid = 25
const arrobj = [
{ id: 10, price: null },
{ id: 12, price: 1 },
{ id: 14, price: 2 },
{ id: 25, price: 3 }, 
{ id: 23, price: 4 },
{ id: 43, price: 5 },
];
// arrobj id is same as paramid, `price` becomes null for `id` 25 and its oldvalue price `3` moves to next object id `23`(oldvalue), then to id `43` price (oldvalue+1)
Expected Output:
[
{ id: 10, price: null },
{ id: 12, price: 1 },
{ id: 14, price: 2 },
{ id: 25, price: null }, 
{ id: 23, price: 3 },
{ id: 43, price: 4 },
];
example 2
let paramid = 43
const arrobj = [
{ id: 10, price: null },
{ id: 13, price: 1 },
{ id: 14, price: 2 },
{ id: 25, price: 5 }, 
{ id: 23, price: 6 },
{ id: 43, price: 4 },
{ id: 28, price: 7 }
];
// arrobj id is same as paramid, `price` becomes null for `id` 43 and its oldvalue price `4` moves to next object id `25`, then to id `23`	
Expected Output:
[
{ id: 10, price: null },
{ id: 12, price: 1 },
{ id: 14, price: 2 },
{ id: 25, price: 4 }, 
{ id: 23, price: 5 },
{ id: 43, price: null },
{ id: 28, price: 6 }
];

// Note: price is always updated to next consecutive price value.

Tried
const getSameId =  arrobj(e=>e.id === paramid);
let oldValue = getSameId.price;
for (let item of arrobj) {
const index = arrobj.index(item)
if(item.id === paramid) {
...item,
price: null 
}
if(item.id !== paramid && oldValue > item.price) {
...item,
price: oldValue + 1 
}
}

答案1

得分: 1

你的代码存在3个不同的问题:

  1. 要根据条件获取匹配的项,实际的语法是 arrayObj.find(e => e.id === paramid)。请注意,只会返回符合条件的第一个值,如果找不到符合条件的值,返回值是 undefined
    (请参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)

  2. 要获取项目的索引,语法是 arrobj.indexOf(item)
    (请参考文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)

  3. 或者,您可以通过执行 arrayObj.findIndex(e => e.id === paramid) 来合并这两个步骤,然后使用该索引更新值。

最后一个问题是您获取后续项目的新值的方式:您将它们的当前价格加1,而不是从前一个项目获取值。

const sameIdIndex = arrObj.findIndex((item) => item.id === paramid);

const updatedArray = arrObj.map((item, index) => {
  if (index === sameIdIndex) {
    return { ...item, price: null };
  }
  if (index > sameIdIndex) {
    const indexToGetFrom = sameIdIndex + (index - sameIdIndex) - 1;
    return { ...item, price: arrObj[indexToGetFrom].price };
  }
  return item;
});

(您仍然可以使用 for 来完成这个任务,我只是使用 Array.map(),因为我个人更喜欢它)

英文:

Your code has 3 different issues:

To get an item matching a condition the syntax is actually arrayObj.find(e => e.id === paramid). Note that only the first value matching the criteria will be returned, and that the returned value is undefined if no value matching the criteria is found.
(See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)

And the syntax to get the index of an item is arrobj.indexOf(item).
(See documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)

Or you could merge these 2 steps by doing arrayObj.findIndex(e => e.id === paramid), and then update the values using that index.

The last issue is the way you get the new value for subsequent items: you're adding 1 to their current price, instead of getting the value from a previous item.

const sameIdIndex = arrObj.findIndex((item) => item.id === paramid);

const updatedArray = arrObj.map((item, index) => {
  if (index === sameIdIndex) {
    return { ...item, price: null };
  }
  if (index > sameIdIndex) {
    const indexToGetFrom = sameIdIndex + (index - sameIdIndex) - 1;
    return { ...item, price: arrObj[indexToGetFrom].price };
  }
  return item;
});

(you can still do that with a for, I just use Array.map() because I personally prefer it)

huangapple
  • 本文由 发表于 2023年6月16日 14:51:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定