Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce 函数问题 #1

Open
kele59 opened this issue Jan 18, 2017 · 4 comments
Open

Reduce 函数问题 #1

kele59 opened this issue Jan 18, 2017 · 4 comments

Comments

@kele59
Copy link
Owner

kele59 commented Jan 18, 2017

@smartphp
看到了你的问题,比较忙,简单的分析下:

var Types = {
  NUMBER: 'NUMBER',
  DECIMAL: 'DECIMAL',
  SIGN: 'SIGN'
}

var inputs = [
  {value: 1, type: Types.NUMBER},
  {value: 2, type: Types.NUMBER},
  {value: 3, type: Types.NUMBER},
  {value: 4, type: Types.NUMBER},
  {value: 5, type: Types.NUMBER},
  {value: 6, type: Types.NUMBER},
  {value: 7, type: Types.NUMBER},
  {value: 8, type: Types.NUMBER},
  {value: 9, type: Types.NUMBER},
  {value: '+/-', type: Types.SIGN},
  {value: 0, type: Types.NUMBER},
  {value: '.', type: Types.DECIMAL},
];

var result = inputs.reduce((collection, input) => {
      if (collection[collection.length - 1].length === 3) {
        collection.push([]);
      }
      collection[collection.length-1].push(input);
      return collection;
    }, [[]])

console.log(result)
console.table(result)

image

@phpsmarter
Copy link

明白了,多谢! 其实问题出在 这个 initialVaule [[]] .二维数组很巧妙。
这个函数在遍历的时候功能很强大,受教了。

@phpsmarter
Copy link

var str =name, age, hair\nMerble, 35, red\nBob, 64, blonde; var result = str.split('\n').reduce((collection, input) => { if (collection[collection.length - 1].length === 3) { collection.push([]); } collection[collection.length-1].push(input); //console.log(collection); return collection; }, [[]]);

我用这个方法把你的lameCSV函数改造了一下,不知道这么行不行啊

@phpsmarter
Copy link

spilt函数处理的字符串只有三个数组元素,不太一样
result[0][0],result[0][1],result[0][2] 倒是可以取出来元素

@kele59
Copy link
Owner Author

kele59 commented Jan 18, 2017

@phpsmarter
在控制台里试试,�看看结果

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants