小编Dze*_* H.的帖子

迭代一个对象数组,使用相同的索引对值求和,并返回一个新的对象数组

我有一个对象数组,如下所示:

const data = [                 // array1
  [{x: 1}, {y:2}, {z:3}], 
  [{x: 1}, {y:2}, {z:3}],
  [{x: 1}, {y:2}, {z:3}]
],[                            // array2
  [{x: 1}, {y:2}, {z:3}], 
  [{x: 1}, {y:2}, {z:3}],
  [{x: 1}, {y:2}, {z:3}]
]
Run Code Online (Sandbox Code Playgroud)

有什么需要完成总结是xarray1x来自array2具有相同指数.这同样适用于yz.最终结果应该是包含求和值的新对象数组.

像这样的东西:

[
  [{totalXOne: 2}, {totalYOne: 4}, {totalZOne: 6}],
  [{totalXTwo: 2}, {totalYTwo: 4}, {totalZTwo: 6}],
  [{totalXThree: 2}, {totalYthree: 4}, {totalZThree: 6}],
]
Run Code Online (Sandbox Code Playgroud)

注意:所有数组的长度都相同,如果缺少一个值,它将替换为0)

我发现MDN一件好事,但它汇总所有x,y,z价值观,它的单一返回汇总值,就像这样: …

javascript arrays reduce object node.js

6
推荐指数
1
解决办法
131
查看次数

如何修复此错误 -npm

我尝试安装 npm,但得到以下内容error。我已经删除了 npm 并重新安装了它,但我再次收到以下错误:


npm ERR! Linux 4.15.0-30-generic
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "start"
npm ERR! node v8.10.0
npm ERR! npm  v3.5.2
npm ERR! code ELIFECYCLE
npm ERR! vx-leave-system@0.0.1 start: `node ./tools/server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the vx-leave-system@0.0.1 start script 'node ./tools/server.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem …
Run Code Online (Sandbox Code Playgroud)

node.js npm npm-install

4
推荐指数
2
解决办法
1万
查看次数

根据具有特定长度的现有字符串获取新字符串

我在弄清楚如何根据现有字符串生成新字符串时遇到问题。新创建的字符串应该有一个特定的length. 如果 a 只能使用内置repeat函数重复提供的字符串,那会容易得多,但这不会产生关于其长度的预期结果。

我有以下字符串:

const str = 'abc';
Run Code Online (Sandbox Code Playgroud)

我需要重复它n的次数:

const desiredLength = 10; // e.g.
Run Code Online (Sandbox Code Playgroud)

所以,我尝试了这样的事情:

const string = "abc";
const len = 10;

// I took the string and made it into a array of characters
const strArr = string.split('');
// I created a new (empty) arry
const newArr = [];

// I used a for-in loop
for(let i in strArr){
// Implemented a simple condition
  while(newArr.length < len) {
// And …
Run Code Online (Sandbox Code Playgroud)

javascript arrays string for-loop

1
推荐指数
1
解决办法
45
查看次数

映射两个数组并在单个表中显示数据?

假设我有两个数组形式的状态属性,如下所示:

constructor(props) {
    super(props);
    this.state = {
            fullNames: [],
            salaryInfo: []
        }}
Run Code Online (Sandbox Code Playgroud)

...等等(我收到 props 的许多其他代码,发出 ajax 请求)

我想要完成的是循环遍历我的状态中的这两个数组并将它们显示在一个table中。

例如,很容易做这样的事情:

const employeesFormated = fullNames
        .map(item => {
        return (
        <tr>
            <td>{item}</td>
        </tr>

const salariesFormated = salaryInfo
        .map(item => {
        return (
        <tr>
            <td>{item}</td>
        </tr>
Run Code Online (Sandbox Code Playgroud)

...然后将其显示在 JSX 表体中:

                   <table>
                       <thead>
                        <tr>
                            <th>Employee name</th>                        
                        </tr>
                        </thead>
                        <tbody>
                            {employeesFormated}
                        </tbody>
                    </table>
Run Code Online (Sandbox Code Playgroud)

...对于工资数组/表也可以做同样的事情。通过一些额外的 CSS 技巧,也许可以将两个不同的表放置在正确的位置,但我想知道是否可以映射这两个表并将它们呈现在具有相应数据的单个表中,这意味着 =>

员工A === 工资A

javascript arrays html-table reactjs

0
推荐指数
1
解决办法
4011
查看次数