0

如果属性存在,我试图覆盖数组中的对象title,否则只需将其推送到数组中。我找到了两种方法,我想知道哪一种是首选。

性能并不是真正的问题,但我想知道可变性是否可能,或者只是有更好的方法来完全做到这一点。

在这个片段中,我使用 for 循环来编辑原始数组:

const data = [
  {
    title: 'AAA',
    people: [ 'John', 'Megan',]
  },{
    title: 'BBB',
    people: [ 'Emily', 'Tom']
  }
]

// If inputTitle is not on any of data's title it will append not overwrite
// e.g. const inputTitle = 'CCC'
const inputTitle = 'AAA'
const inputPeople = ['Peter', 'Jane']

for (const obj of data) {
  if (obj.title === inputTitle) {
    obj.people = inputPeople
    break
  } else {
    data.push({
      title: inputTitle,
      people: inputPeople
    })
    break
  }
}

console.log(data)

在这里,我使用高阶函数并展开来做同样的事情:

const data = [
  {
    title: 'AAA',
    people: [ 'John', 'Megan',]
  },{
    title: 'BBB',
    people: [ 'Emily', 'Tom']
  }
]

// If inputTitle is not on any of data's title it will append not overwrite
// e.g. const inputTitle = 'CCC'
const inputTitle = 'AAA'
const inputPeople = ['Peter', 'Jane']

let res = []

if (data.some(({ title }) => title === inputTitle)) {
  res = data.map(obj => {
    if (obj.title === inputTitle) 
      obj.people = inputPeople
    return obj
  })
} else {
  res = [...data, { title: inputTitle, people: inputPeople}]
}

console.log(res)

在实际任务中,我正在data使用节点从 json 文件中读取数组并将更改写回它。

4

3 回答 3

1
let newdata = {title: 'AAA',people: [ 'Peter', 'Jane']}
//Create new object 

data.find(c => c.title === newdata.title) !== undefined 
// if title data from new object is find in old
? data.find(c => c.title === newdata.title).people=newdata.people
// change data
 : data.push(newdata)
// in not push it as new

const data = [
  {
    title: 'CCC',
    people: [ 'John', 'Megan']
  },{
    title: 'BBB',
    people: [ 'Emily', 'Tom']
  }
]


let newdata = {title: 'AAA',people: [ 'Peter', 'Jane']}

data.find(c => c.title === newdata.title) !== undefined ? data.find(c => c.title === newdata.title).people=newdata.people : data.push(newdata)

console.log(data)

于 2021-03-06T15:14:30.833 回答
1

如果这是一个常见的用例,您将在同一个数据结构上执行多次,那么您最好使用一个以标题为键的普通对象,因为这样操作很简单。您仍然可以将所有权保留为您拥有的财产。

const data = {
  AAA: {
    title: 'AAA',
    people: [ 'John', 'Megan',]
  },
  BBB: {
    title: 'BBB',
    people: [ 'Emily', 'Tom']
  }
};

const title = 'AAA';
const people = ['Peter', 'Jane'];

data[title] = { title, people };  // yes, it's that simple then...

console.log(data);

如果你真的需要数组结构,那么你可以考虑暂时切换,然后做所有的操作,然后回到原来的格式:

let data = [
  {
    title: 'AAA',
    people: [ 'John', 'Megan',]
  },
  {
    title: 'BBB',
    people: [ 'Emily', 'Tom']
  }
];

const title = 'AAA';
const people = ['Peter', 'Jane'];

// switch to other data structure...
let map = Object.fromEntries(data.map(o => [o.title, o]));

// Manipulate
map[title] = { title, people };

// ...and back:
data = Object.values(map);

console.log(data);

于 2021-03-06T15:35:20.777 回答
0

如果只需要更新匹配的项目,也许一个forEach可以解决这个问题。

const data = [
  {
    title: 'AAA',
    people: [ 'John', 'Megan',]
  },{
    title: 'BBB',
    people: [ 'Emily', 'Tom']
  }
]

const inputTitle = 'AAA'
const inputPeople = ['Peter', 'Jane']

data.forEach(({ title }, index) => title === inputTitle ? data[index].people = inputPeople : null)

console.log(data)

于 2021-03-06T15:13:34.597 回答