1

我正在尝试开发一个具有“传输”专业知识的数组,其中包含每个扇区和分配给它们的文本。

这就是我所拥有的:

    - 卫生保健
    -- 数据驱动的洞察力以改善医疗保健
    -- 新颖的数据挖掘和可视化
    -- 以人为本的增强和虚拟现实

    - 活力
    -- 城市分析
    -- 了解消费者行为模式
    -- 优化能源系统

这就是我想要做的:

    - 专业知识(例如运输)

    -- 部门(例如数据)
    --- 文本(例如城市分析)
    --- 文本(例如了解消费者行为模式)
    --- 文本(例如以人为中心的增强和虚拟现实)

    -- 部门(例如工程)
    --- 文本(例如优化能源系统)
    --- 正文(如高铁与系统集成创新)
    --- 文本(例如以人为中心的机器人和自主系统)
    --- 文本(例如跨长度尺度的表面工程)

    -- 部门(例如环境)
    ---文本(例如欧洲领先的城市模拟能力)

    - 专业知识
    -- 部门
    - - 文本

这是我目前正在使用的代码,如果有人能够提供帮助,我将不胜感激。

var items = [{
    "item": {
      "id": 0,
      "sector": 'Data',
      "expertise": ["Healthcare"],
      "text": "Data-driven insights to improve healthcare"
    }
  },
  {
    "item": {
      "id": 1,
      "sector": 'Data',
      "expertise": ["Energy", "Transport", "Cities"],
      "text": "Urban Analytics"
    }
  },
  {
    "item": {
      "id": 2,
      "sector": 'Data',
      "expertise": ["Energy", "Transport", "Consumer"],
      "text": "Understanding patterns of consumer behaviour"
    }
  },
  {
    "item": {
      "id": 3,
      "sector": 'Data',
      "expertise": ["Healthcare", "Consumer"],
      "text": "Novel data mining and visualisation"
    }
  },
  {
    "item": {
      "id": 4,
      "sector": 'Data',
      "expertise": ["Healthcare", "Transport", "Consumer"],
      "text": "Human-centred augmented and virtual realities"
    }
  },
  {
    "item": {
      "id": 5,
      "sector": 'Healthcare',
      "expertise": ["Healthcare"],
      "text": "Medical technology innovation, translation and convergence"
    }
  },
  {
    "item": {
      "id": 6,
      "sector": 'Healthcare',
      "expertise": ["Healthcare"],
      "text": "Understanding cardio metabolic disease"
    }
  },
  {
    "item": {
      "id": 7,
      "sector": 'Healthcare',
      "expertise": ["Healthcare"],
      "text": "Improving early cancer diagnosis and treatment"
    }
  },
  {
    "item": {
      "id": 8,
      "sector": 'Healthcare',
      "expertise": ["Healthcare", "Pharmaceuticals"],
      "text": "Understanding life in molecular detail"
    }
  },
  {
    "item": {
      "id": 9,
      "sector": 'Healthcare',
      "expertise": ["Healthcare"],
      "text": "Evidencing treatment through clinical trials"
    }
  },
  {
    "item": {
      "id": 10,
      "sector": 'Engineering',
      "expertise": ["Energy", "Transport"],
      "text": "Optimising energy systems"
    }
  },
  {
    "item": {
      "id": 11,
      "sector": 'Engineering',
      "expertise": ["Energy"],
      "text": "Enhancing petroleum recovery"
    }
  },
  {
    "item": {
      "id": 12,
      "sector": 'Engineering',
      "expertise": ["Transport"],
      "text": "High speed rail and system integration innovation"
    }
  },
  {
    "item": {
      "id": 13,
      "sector": 'Engineering',
      "expertise": ["Healthcare", "Electronics"],
      "text": "Terahertz frequency electronic and photonic devices"
    }
  },
  {
    "item": {
      "id": 14,
      "sector": 'Engineering',
      "expertise": ["Healthcare", "Transport", "Manufacturing"],
      "text": "Human-centred robotic and autonomous systems"
    }
  },
  {
    "item": {
      "id": 15,
      "sector": 'Engineering',
      "expertise": ["Healthcare", "Energy", "Transport", "Manufacturing"],
      "text": "Surface engineering across the length scales"
    }
  },
  {
    "item": {
      "id": 16,
      "sector": 'Engineering',
      "expertise": ["Pharmaceuticals", "Manufacturing"],
      "text": "Chemical and process engineering from molecule to product"
    }
  },
  {
    "item": {
      "id": 17,
      "sector": 'Engineering',
      "expertise": ["Pharmaceuticals"],
      "text": "Bionanotechnology for disease diagnosis, treatment and prevention"
    }
  },
  {
    "item": {
      "id": 18,
      "sector": 'Engineering',
      "expertise": ["Healthcare", "Pharmaceuticals", "Electronics", "Manufacturing"],
      "text": "Engineering materials at the atomic level"
    }
  },
  {
    "item": {
      "id": 19,
      "sector": 'Environment',
      "expertise": ["Healthcare", "Environment & Food"],
      "text": "Addressing global challenges in food security"
    }
  },
  {
    "item": {
      "id": 20,
      "sector": 'Environment',
      "expertise": ["Healthcare", "Environment & Food"],
      "text": "Interdisciplinary approaches to tackling major water issues"
    }
  },
  {
    "item": {
      "id": 21,
      "sector": 'Environment',
      "expertise": ["Cities"],
      "text": "Designing cities of the future"
    }
  },
  {
    "item": {
      "id": 22,
      "sector": 'Environment',
      "expertise": ["Transport", "Cities"],
      "text": "Europe’s leading city simulation capability"
    }
  },
  {
    "item": {
      "id": 23,
      "sector": 'Environment',
      "expertise": ["Environment & Food"],
      "text": "Robust and timely climate solutions"
    }
  }
];

let expertise;
let res = items.reduce((acc, {
  item: {
    expertise,
    text
  }
}) => {
  expertise.forEach(x => acc[x] = [...(acc[x] || []), text]);
  return acc;
}, {});


Object.entries(res).forEach(([k, v]) => {
  console.log(k + "\n->" + v.join("\n->"));
});

4

2 回答 2

3

您需要为这些部门增加一个级别:

var items = [{"item": {"id": 0,"sector": 'Data',"expertise": ["Healthcare"],"text": "Data-driven insights to improve healthcare"}},{"item": {"id": 1,"sector": 'Data',"expertise": ["Energy", "Transport", "Cities"],"text": "Urban Analytics"}},{"item": {"id": 2,"sector": 'Data',"expertise": ["Energy", "Transport", "Consumer"],"text": "Understanding patterns of consumer behaviour"}},{"item": {"id": 3,"sector": 'Data',"expertise": ["Healthcare", "Consumer"],"text": "Novel data mining and visualisation"}},{"item": {"id": 4,"sector": 'Data',"expertise": ["Healthcare", "Transport", "Consumer"],"text": "Human-centred augmented and virtual realities"}},{"item": {"id": 5,"sector": 'Healthcare',"expertise": ["Healthcare"],"text": "Medical technology innovation, translation and convergence"}},{"item": {"id": 6,"sector": 'Healthcare',"expertise": ["Healthcare"],"text": "Understanding cardio metabolic disease"}},{"item": {"id": 7,"sector": 'Healthcare',"expertise": ["Healthcare"],"text": "Improving early cancer diagnosis and treatment"}},{"item": {"id": 8,"sector": 'Healthcare',"expertise": ["Healthcare", "Pharmaceuticals"],"text": "Understanding life in molecular detail"}},{"item": {"id": 9,"sector": 'Healthcare',"expertise": ["Healthcare"],"text": "Evidencing treatment through clinical trials"}},{"item": {"id": 10,"sector": 'Engineering',"expertise": ["Energy", "Transport"],"text": "Optimising energy systems"}},{"item": {"id": 11,"sector": 'Engineering',"expertise": ["Energy"],"text": "Enhancing petroleum recovery"}},{"item": {"id": 12,"sector": 'Engineering',"expertise": ["Transport"],"text": "High speed rail and system integration innovation"}},{"item": {"id": 13,"sector": 'Engineering',"expertise": ["Healthcare", "Electronics"],"text": "Terahertz frequency electronic and photonic devices"}},{"item": {"id": 14,"sector": 'Engineering',"expertise": ["Healthcare", "Transport", "Manufacturing"],"text": "Human-centred robotic and autonomous systems"}},{"item": {"id": 15,"sector": 'Engineering',"expertise": ["Healthcare", "Energy", "Transport", "Manufacturing"],"text": "Surface engineering across the length scales"}},{"item": {"id": 16,"sector": 'Engineering',"expertise": ["Pharmaceuticals", "Manufacturing"],"text": "Chemical and process engineering from molecule to product"}},{"item": {"id": 17,"sector": 'Engineering',"expertise": ["Pharmaceuticals"],"text": "Bionanotechnology for disease diagnosis, treatment and prevention"}},{"item": {"id": 18,"sector": 'Engineering',"expertise": ["Healthcare", "Pharmaceuticals", "Electronics", "Manufacturing"],"text": "Engineering materials at the atomic level"}},{"item": {"id": 19,"sector": 'Environment',"expertise": ["Healthcare", "Environment & Food"],"text": "Addressing global challenges in food security"}},{"item": {"id": 20,"sector": 'Environment',"expertise": ["Healthcare", "Environment & Food"],"text": "Interdisciplinary approaches to tackling major water issues"}},{"item": {"id": 21,"sector": 'Environment',"expertise": ["Cities"],"text": "Designing cities of the future"}},{"item": {"id": 22,"sector": 'Environment',"expertise": ["Transport", "Cities"],"text": "Europe’s leading city simulation capability"}},{"item": {"id": 23,"sector": 'Environment',"expertise": ["Environment & Food"],"text": "Robust and timely climate solutions"}}];

const res = items.reduce((acc, {item: {sector, expertise, text}}) => {
    expertise.forEach(exp => {
        acc[exp] = acc[exp] || {};
        acc[exp][sector] = [...(acc[exp][sector] || []), text];
    });
    return acc;
}, {});

Object.entries(res).forEach(([exp, sectors]) => {
    console.log("- " + exp);
    Object.entries(sectors).forEach(([sector, texts]) => {
        console.log("-- " + sector + "\n--- " + texts.join("\n--- "));
    });
});

解释

reduce操作中,sector也是从对象中抓取的。就像原来的一样,经验是迭代的,但现在acc对象变成了两级对象,而不仅仅是一层。第一级属性是专业知识 ( exp)。如果这样的属性还不存在,它会获取一个空对象作为值:

        acc[exp] = acc[exp] || {};

接下来的行很像您已经拥有的:下一个属性级别由扇区组成,并且为每个扇区维护一个文本数组。

代码的第二部分应用了与您已经拥有的相同的原则,但同样具有额外的级别。首先,迭代专业知识属性。它们将对象作为值,每个对象都具有扇区属性。然后,内部循环再次与您拥有的非常相似:对于每个扇区,都会显示扇区和相关的文本值。

于 2019-01-28T09:44:56.363 回答
-1

您可以尝试以下通用的方法:

主意

  • 创建一个打印的函数(比如 A):
    • 子弹,在这个解决方案中-
    • 正在使用的密钥
    • 此键的特定格式的值。
  • 还要创建一个函数(比如 B)来解析并返回格式化字符串的值。
    • 对于数组,它将使用Array.join
    • 对于非空对象,它将使用您在上面创建的相同函数(A)。
    • 对于非空文字值,它将返回value.toString()
    • 对于空值,它将返回一个空值。

  • 两者都起作用AB表示较低/下一个级别。因此,您必须将-项目符号值添加到现有项目符号字符串中。
  • 由于它是一个嵌套结构,因此您必须清理字符串(Remove extra \ns usingstring.trim()),这样您就不会得到多个新行。
  • 使用这些相互关联的功能,无论深度如何,都将以必要的格式打印。

function isEmpty(value) {
  return value === null || value === undefined
}

function getValueString(v, preString) {
  preString = `\n${preString.trim()}-`;
  return Array.isArray(v) && (preString + v.join(preString)) ||
    typeof v === 'object' && nestedLevelToString(v, preString) ||
    isEmpty(v) && v.toString() ||
    '';
}

function nestedLevelToString(data, preString) {
  preString = preString || '-';
  let output = ''
  for (const key in data) {
    output += `${preString}${key}${getValueString(data[key], preString)}`;
  }
  return output;
}

var items = [{"item": {"id": 0,"sector": 'Data',"expertise": ["Healthcare"],"text": "Data-driven insights to improve healthcare"}},{"item": {"id": 1,"sector": 'Data',"expertise": ["Energy", "Transport", "Cities"],"text": "Urban Analytics"}},{"item": {"id": 2,"sector": 'Data',"expertise": ["Energy", "Transport", "Consumer"],"text": "Understanding patterns of consumer behaviour"}},{"item": {"id": 3,"sector": 'Data',"expertise": ["Healthcare", "Consumer"],"text": "Novel data mining and visualisation"}},{"item": {"id": 4,"sector": 'Data',"expertise": ["Healthcare", "Transport", "Consumer"],"text": "Human-centred augmented and virtual realities"}},{"item": {"id": 5,"sector": 'Healthcare',"expertise": ["Healthcare"],"text": "Medical technology innovation, translation and convergence"}},{"item": {"id": 6,"sector": 'Healthcare',"expertise": ["Healthcare"],"text": "Understanding cardio metabolic disease"}},{"item": {"id": 7,"sector": 'Healthcare',"expertise": ["Healthcare"],"text": "Improving early cancer diagnosis and treatment"}},{"item": {"id": 8,"sector": 'Healthcare',"expertise": ["Healthcare", "Pharmaceuticals"],"text": "Understanding life in molecular detail"}},{"item": {"id": 9,"sector": 'Healthcare',"expertise": ["Healthcare"],"text": "Evidencing treatment through clinical trials"}},{"item": {"id": 10,"sector": 'Engineering',"expertise": ["Energy", "Transport"],"text": "Optimising energy systems"}},{"item": {"id": 11,"sector": 'Engineering',"expertise": ["Energy"],"text": "Enhancing petroleum recovery"}},{"item": {"id": 12,"sector": 'Engineering',"expertise": ["Transport"],"text": "High speed rail and system integration innovation"}},{"item": {"id": 13,"sector": 'Engineering',"expertise": ["Healthcare", "Electronics"],"text": "Terahertz frequency electronic and photonic devices"}},{"item": {"id": 14,"sector": 'Engineering',"expertise": ["Healthcare", "Transport", "Manufacturing"],"text": "Human-centred robotic and autonomous systems"}},{"item": {"id": 15,"sector": 'Engineering',"expertise": ["Healthcare", "Energy", "Transport", "Manufacturing"],"text": "Surface engineering across the length scales"}},{"item": {"id": 16,"sector": 'Engineering',"expertise": ["Pharmaceuticals", "Manufacturing"],"text": "Chemical and process engineering from molecule to product"}},{"item": {"id": 17,"sector": 'Engineering',"expertise": ["Pharmaceuticals"],"text": "Bionanotechnology for disease diagnosis, treatment and prevention"}},{"item": {"id": 18,"sector": 'Engineering',"expertise": ["Healthcare", "Pharmaceuticals", "Electronics", "Manufacturing"],"text": "Engineering materials at the atomic level"}},{"item": {"id": 19,"sector": 'Environment',"expertise": ["Healthcare", "Environment & Food"],"text": "Addressing global challenges in food security"}},{"item": {"id": 20,"sector": 'Environment',"expertise": ["Healthcare", "Environment & Food"],"text": "Interdisciplinary approaches to tackling major water issues"}},{"item": {"id": 21,"sector": 'Environment',"expertise": ["Cities"],"text": "Designing cities of the future"}},{"item": {"id": 22,"sector": 'Environment',"expertise": ["Transport", "Cities"],"text": "Europe’s leading city simulation capability"}},{"item": {"id": 23,"sector": 'Environment',"expertise": ["Environment & Food"],"text": "Robust and timely climate solutions"}}];


let expertise;
const res = {};
res['sector'] = items.reduce((acc, {
  item: {
    expertise,
    text
  }
}) => {
  expertise.forEach(x => acc[x] = [...(acc[x] || []), text]);
  return acc;
}, {});

console.log(nestedLevelToString(res));

于 2019-01-28T10:31:35.287 回答