1

假设我有这两个对象:

let person1 = {name: "Charlie", age: 65, childrenNames:["Ruth", "Charlie Jr."] parentNames: ["Dolph", "Grace"]};

let person2 = {name: "Charlie Jr.", age: 34, childrenNames:[] parentNames: ["Charlie", "Grace"]};

现在假设我想表达 person1 是 person2 的父亲,因此 person2 是 person1 的儿子。也就是说,person1 的属性中的“Charlie Jr”childrenNames是 person2,而 person2 的属性中的“Charlie”parentNames是 person1。

我怎么能做到这一点?我看不出将一个对象嵌入另一个对象如何解决问题,而只是简单地复制它。有没有办法让对象内部的属性成为另一个对象的标识符?

非常感谢!

4

3 回答 3

0

为什么不添加关系索引?将您的人员组合为 1 人阵列。迭代并添加 parentNames,而不是 parentIndexes。这样,您就拥有了索引,而不是按名字查找父母或儿子。

注意,为了让我的例子简单,我只做父子关系。您可以使用完全相同的逻辑轻松地将儿子添加到父关系。

例子

if (peopleArray[i].parentsIndex.length > 0) {
  // get first parent
  //peopleArray[peopleArray[i].parentsIndex[0]];
}

修改您的对象。

let peopleArray = [

  {
    name: "Charlie",
    age: 65,
    parentNames: ["Dolph", "Grace"]
  },
  {
    name: "Grace",
    age: 65,
    parentNames: ["Dolph", "Grace"]
  },
  {
    name: "Dolph",
    age: 65,
    parentNames: ["ADSGS", "Grace"]
  }
];
peopleArray = peopleArray.map(callback.bind(null));

function callback(item) {
    item["parentsIndex"] = [];

  for (let i = 0; i < item.parentNames.length; i++) {
    let parentObj = peopleArray.find(x => x.name === item.parentNames[i]);
    if (parentObj !== undefined) {
      item["parentsIndex"].push(peopleArray.indexOf(parentObj));
    }

  }
  return item;
}
console.log(peopleArray);
// use case 
if (peopleArray[0].parentsIndex.length > 0) {
  // get first parent
  //peopleArray[peopleArray[0].parentsIndex[0]];
}

于 2020-08-31T23:22:42.827 回答
0

例如,如果您想知道某人是否是人 1 的孩子,您可以执行以下操作:

person1.childrenNames.forEach((childrenName) => { 

if(childrenName=== person2.name) {
  console.log(person1.name + ' is the parent of + person2.name);
});

你也可以做一个嵌套函数,这样你就可以检查这个人是否是多人的父母。

于 2020-08-31T22:21:39.223 回答
0

我想这取决于您的场景有多复杂以及您想要实现什么,但假设您为relations. 该表可以保存有关 2 个人共享的关系类型的信息,然后可以用于查找该数据。

例如,如果我们有 4 个人,其中 2 个是父母(Charlie & Grace),1 个是儿子(Charlie Jr),我们可以形成如下关系表。

我们不需要指出小查理是儿子,因为我们已经知道孩子的父母。

const familyDb = {
  persons: [
    { id: 1, name: 'Charlie', age: 68 },
    { id: 2, name: 'Grace', age: 64 },
    { id: 3, name: 'Charlie Jr', age: 34 },
    { id: 4, name: 'Grace', age: 36 }
  ],
  relations: [
    { id: 1, person1: 1, person2: 2, type: 'spouse', from: 1970, till: null },
    { id: 2, person1: 3, person2: 4, type: 'spouse', from: 2010, till: null },
    { id: 3, person1: 1, person2: 3, type: 'parent' },
    { id: 3, person1: 2, person2: 3, type: 'parent' }
  ]
};

function getParents( person ) {
  return familyDb.relations.filter( relation => relation.person2 === person.id && relation.type === 'parent' );
}

function getChildren( person ) {
  return familyDb.relations.filter( relation => relation.person1 === person.id && relation.type === 'parent' );
}


console.log( getParents( familyDb.persons[2] ) );
console.log( getChildren( familyDb.persons[0] ) );

所以上面的代码对此采取了一种基本的方法,你有:

  • 唯一id标识一个人(在您的示例中名称匹配会很困难,因为格蕾丝既是查理和小查理的妈妈)
  • 标识type两个人之间某些关系的表格

之后,您只需要一种从数据集中查找信息的方法,就可以开始使用

于 2020-08-31T22:29:27.563 回答