使用另一个对象的索引填充对象

时间:2017-08-02 11:34:05

标签: javascript arrays d3.js

我有两个对象。一个是单词数组,另一个是第一个数组中两个单词之间的关系:

nodes = ["apple","cherry","pear","strawberry"]

data = [{word1 : "apple", word2 : "cherry" , weight : "0.1"},
        {word1 : "apple", word2 : "strawberry" , weight : "0.2"},
        {word1 : "cherry", word2 : "pear" , weight : "0.3"},
        {word1 : "cherry", word2 : "strawberry" , weight : "0.4"},
        {word1 : "strawberry", word2 : "pear" , weight : "0.5"}]

我想创建一个具有数据结构的新对象,而不是单词,而是带有索引。像这样:

links =  [{source : "0", target : "1" , value : "0.1"},
          {source : "0", target : "3" , value : "0.2"},
          {source : "1", target : "2" , value : "0.3"},
          {source : "1", target : "3" , value : "0.4"},
          {source : "3", target : "2" , value : "0.5"}]

到目前为止,我有这个:

for (var i = 0 ; i < nodes.length; i++) {
     test[i] = nodes.findIndex(d => d == nodes[i]);
}

这给了我这个新数组,对应于nodes的索引:

test = [0,1,2,3]

那么我现在该如何将这些索引分配给links

中的属性

4 个答案:

答案 0 :(得分:3)

尝试:

var nodes = ["apple","cherry","pear","strawberry"];

var data = [{word1 : "apple", word2 : "cherry" , weight : "0.1"},
        {word1 : "apple", word2 : "strawberry" , weight : "0.2"},
        {word1 : "cherry", word2 : "pear" , weight : "0.3"},
        {word1 : "cherry", word2 : "strawberry" , weight : "0.4"},
        {word1 : "strawberry", word2 : "pear" , weight : "0.5"}];

var links = data.map((item) => {
    return {
        source: nodes.indexOf(item.word1),
        target: nodes.indexOf(item.word2),
        value: item.weight
        
    };
});

console.log(links);
console.log(data);

答案 1 :(得分:1)

您可以在第二个数组(而不是第一个)和Array#map上使用Object.assign,以避免原始数据发生变异。

为了减少时间复杂度,可以创建Map,这样可以在恒定时间内获取索引。该地图可以通过this传递给地图方法。通过检查每个属性的匹配项,您可以将其设置为通用属性,因此算法本身中没有硬编码的word1word2

&#13;
&#13;
const nodes = ["apple","cherry","pear","strawberry"]

const data = [
    {word1 : "apple", word2 : "cherry" , weight : "0.1"},
    {word1 : "apple", word2 : "strawberry" , weight : "0.2"},
    {word1 : "cherry", word2 : "pear" , weight : "0.3"},
    {word1 : "cherry", word2 : "strawberry" , weight : "0.4"},
    {word1 : "strawberry", word2 : "pear" , weight : "0.5"}
];
   
const result = data.map(function(o) {
    return Object.assign(...Object.keys(o).map(key => ({
        [key]: this.has(o[key]) ? this.get(o[key]) : o[key]
    })));
}, new Map(nodes.map( (s,i) => [s,i] ))); 

console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 2 :(得分:0)

使用Array#map()创建新阵列。获取indexOf(word1)indexOf(word2)来替换当前值

&#13;
&#13;
const nodes = ["apple","cherry","pear","strawberry"];

const data = [{word1 : "apple", word2 : "cherry" , weight : "0.1"},
        {word1 : "apple", word2 : "strawberry" , weight : "0.2"},
        {word1 : "cherry", word2 : "pear" , weight : "0.3"},
        {word1 : "cherry", word2 : "strawberry" , weight : "0.4"},
        {word1 : "strawberry", word2 : "pear" , weight : "0.5"}];
        
const updatedData = data.map((item) => ({
  source: nodes.indexOf(item.word1),
  target: nodes.indexOf(item.word2),
  weight: item.weight
}));

console.log(updatedData);
&#13;
&#13;
&#13;

答案 3 :(得分:0)

这样可行:

nodes = ["apple", "cherry", "pear", "strawberry"]

data = [{
    word1: "apple",
    word2: "cherry",
    weight: "0.1"
  },
  {
    word1: "apple",
    word2: "strawberry",
    weight: "0.2"
  },
  {
    word1: "cherry",
    word2: "pear",
    weight: "0.3"
  },
  {
    word1: "cherry",
    word2: "strawberry",
    weight: "0.4"
  },
  {
    word1: "strawberry",
    word2: "pear",
    weight: "0.5"
  }
]

var links = [];

for (var i = 0; i < data.length; i++) {
  links.push({
    source: nodes.indexOf(data[i].word1),
    target: nodes.indexOf(data[i].word2),
    value: data[i].weight
  })
}

console.log(links);