JavaScript数组分组

时间:2018-10-06 08:32:43

标签: javascript arrays grouping

嗨,我正在尝试通过合并同级来对数组进行分组,如果它具有相同的键且具有相同的值

      let array = [
        {el: 1, ready: true},
        {el: 2, ready: false},
        {el: 3, ready: false},
        {el: 4, ready: false},
        {el: 5, ready: true},
        {el: 6, ready: true},
        {el: 7, ready: false},
      ];

想要这样映射

      let arrayGroup = [
        [
          {el: 1, ready: true},
        ],
        [
          {el: 2, ready: false},
          {el: 3, ready: false},
          {el: 4, ready: false}
        ],
        [
          {el: 5, ready: true},
          {el: 6, ready: true}
        ],
        [
          {el: 7, ready: false}
        ]
      ];

4 个答案:

答案 0 :(得分:1)

您可以按照我的方法进行操作

> xy
[1] "Jan 01 2018"           "It's a nice day."      ""                      "Jan 02 2018"          
[5] "Today is a rainy day."

答案 1 :(得分:0)

这应该有效:

let array = [{
    el: 1,
    ready: true
  },
  {
    el: 2,
    ready: false
  },
  {
    el: 3,
    ready: false
  },
  {
    el: 4,
    ready: false
  },
  {
    el: 5,
    ready: true
  },
  {
    el: 6,
    ready: true
  },
  {
    el: 7,
    ready: false
  },
];

let previousArrValue = array[0].ready;
let newArrayBuilt = [];
let newArr = [];

for (let i = 0; i < array.length; i++) {


  if (array[i].ready !== previousArrValue) {
    newArrayBuilt.push(newArr);
    newArr = [];
  }


  previousArrValue = array[i].ready;
  newArr.push(array[i]);

  if (i + 1 === array.length) {
    newArrayBuilt.push(newArr);
    break;
  }


}

console.log(newArrayBuilt)

答案 2 :(得分:0)

在这种情况下, import React, { Component } from 'react'; import './Home.css'; import { Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap'; export class Home extends Component { constructor(props) { super(props); let $this = this; $this.toggle = $this.toggle.bind($this); $this.state = { dropdownOpen: false }; } toggle() { this.setState(prevState => ({ dropdownOpen: !prevState.dropdownOpen })); } render() { return ( <div className="table-div table-responsive-xl"> <table className="table table-hover"> <thead> <tr> <th scope="col" /> <th scope="col">Authentication</th> </tr> </thead> <tbody> {this.state.slackmembers.map((item, key) => { return ( <tr key={key}> <td scope="row" /> <td>{item.Authentication}</td> <td> <Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}> <DropdownToggle className="menu-button"> <i className="fa fa-ellipsis-h" aria-hidden="true" type="ellipsis" /> </DropdownToggle> <DropdownMenu> <DropdownItem style={{ fontWeight: 500, color: 'black' }}>First</DropdownItem> <DropdownItem style={{ fontWeight: 500, color: 'black' }}>Second</DropdownItem> <DropdownItem divider /> <DropdownItem style={{ fontWeight: 500, color: 'red' }}>Last </DropdownItem> </DropdownMenu> </Dropdown> </td> </tr> ); })} </tbody> </table> </div> ); } 可能会有所帮助。对于每个访问的对象,它返回一个Array.reduce属性,该属性成为下一个累加器。

ready

let starter = !array[0].ready; let new_arr = []; let new_index = -1; array.reduce((acc, cur, index, arr) => { if (acc != arr[index].ready) { new_arr.push([cur]); new_index++; } else { new_arr[new_index].push(cur); } return cur.ready; }, starter); 是您需要的数组。在这种情况下,需要一个初始值(new_arr),以便starter函数以创建新的子数组开始。

答案 3 :(得分:0)

 if((charAtIndexTwo=='A')||(charAtIndexTwo=='E')||(charAtIndexTwo=='I')||(charAtIndexTwo=='O')||(charAtIndexTwo=='U')||(charAtIndexTwo=='Y'))

{
    System.out.println("Invalid");
}else
{
    System.out.println((charAtIndexTwo != 'A'));
    if ((totalOne % 2 == 0) && (totalTwo % 2 == 0) && (totalThree % 2 == 0) && (totalFour % 2 == 0)) {
        System.out.println("Valid");
    } else {
        System.out.println("invalid");
    }
}