【问题标题】:Fill an object with indexes from another object用另一个对象的索引填充一个对象
【发布时间】:2017-08-02 11:34:05
【问题描述】:

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

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 中的属性?

【问题讨论】:

    标签: javascript arrays d3.js


    【解决方案1】:

    尝试:

    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);

    【讨论】:

      【解决方案2】:

      您可以在第二个数组(而不是第一个)上使用Array#mapObject.assign 以避免原始数据的突变。

      为了降低时间复杂度,可以创建一个Map,它允许在恒定时间内获取索引。该 Map 可以将this 传递给 map 方法。通过检查每个属性的匹配,您可以使其具有通用性,因此算法本身没有硬编码word1word2

      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);
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      【讨论】:

        【解决方案3】:

        这可行:

        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);

        【讨论】:

          【解决方案4】:

          使用Array#map() 创建一个新数组。获取indexOf(word1)indexOf(word2)替换当前值

          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);

          【讨论】:

            猜你喜欢
            • 2018-03-19
            • 2019-05-18
            • 2022-11-26
            • 1970-01-01
            • 2017-03-02
            • 1970-01-01
            • 1970-01-01
            • 2013-02-18
            • 2012-11-25
            相关资源
            最近更新 更多