如何从该对象数组获得以下输出?

时间:2018-11-10 11:56:12

标签: javascript arrays object ecmascript-6

这是对象的原始数组:

func1 <- function(dataC, PR, DB, MT){

  c1 <- dataC[[1]]
  c2 <- dataC[[2]]
  c3 <- dataC[[3]]
  c4 <- dataC[[4]]

  fun  <- if(MT=="test_1") mean else if(MT=="test_2") harmonic.mean
  fun2 <- function(size,mult)
    fun(sample(1:10, size = size, replace = TRUE)) * mult

  pr_sq <- PR^2
  pr_3 <- 3*PR
  sqrt_2_DB <- sqrt(2) * DB
  V1 <- fun2(pr_sq, sqrt_2_DB)
  V2 <- fun2(pr_3, DB)
  V3 <- fun2(pr_sq, sqrt_2_DB)
  V4 <- fun2(pr_3, DB)
  V5 <- 0
  V6 <- fun2(pr_3,  DB)
  V7 <- fun2(pr_sq, sqrt_2_DB)
  V8 <- fun2(pr_3,  DB)
  V9 <- fun2(pr_sq, sqrt_2_DB)

  inv <- 1/c(V1, V2, V3, V4, V6, V7, V8, V9)
  tot <- sum(inv, na.rm = TRUE)
  mat_V <- matrix(data = c(inv[1:4], V5, inv[5:8]) / tot, 
                  nrow = 3, ncol = 3, byrow = TRUE)

  newC <- NULL
  while(is.null(newC) || identical(c(c3,c4), newC)){

    if(identical(c(c3,c4), newC)){
      mat_V[choiceC[1], choiceC[2]] <- NaN
      ## print(mat_V)
    }

    choiceC <- which(mat_V == max(mat_V, na.rm = TRUE), arr.ind = TRUE)
    ## print(choiceC)
    ## If there are several maximum values
    if(nrow(choiceC) > 1){
      choiceC <- choiceC[sample(1:nrow(choiceC), 1), ]
    }

    newC <- c(c1 - 2 + choiceC[2], c2 + 2 - choiceC[1])

    # using switch it would have been
    # newC <- switch(choiceC[1],
    #        `1` = switch(choiceC[2],
    #                     `1` = c(x = c1 - 1, y = c2 + 1),
    #                     `2` = c(x = c1, y = c2 + 1),
    #                     `3` = c(x = c1 + 1, y = c2 + 1)),
    #        `2` = switch(choiceC[2],
    #                     `1` = c(x = c1 - 1, y = c2),
    #                     `2` = c(x = c1, y = c2), # you were missing this one
    #                     `3` = c(x = c1 + 1, y = c2)),
    #        `3` = switch(choiceC[2],
    #                     `1` = c(x = c1 - 1, y = c2 - 1),
    #                     `2` = c(x = c1, y = c2 - 1),
    #                     `3` = c(x = c1 + 1, y = c2 - 1)))
  }
  t(newC)
}

我需要输出以下内容:

const input = [

    {firstProperty: 'something', 
    money: 3, 
    user: 'john-smith'}, 
    {firstProperty: 'somethingDiff', 
    money: 7, 
    user: 'john-smith'}, 
    {firstProperty: 'somElse', 
    money: 14, 
    user: 'jane-doe'},
    {firstProperty: 'someOtherThing', 
    money: 2, 
    user: 'jane-doe'}]

我已经通过使用一堆for循环实现了这一结果,但我一直在寻找一种优雅的解决方案,该解决方案仅使用map,filter,reduce等ES6方法。

2 个答案:

答案 0 :(得分:2)

使用更新的代码,可以更轻松地获得结果,我的方法仍然是先聚合然后映射。

我对您原始帖子的每次更改都更新了代码。

const input = [
    {firstProperty: 'something', 
    money: 3, 
    user: 'john-smith'}, 
    {firstProperty: 'somethingDiff', 
    money: 7, 
    user: 'john-smith'}, 
    {firstProperty: 'somElse', 
    money: 14, 
    user: 'jane-doe'},
    {firstProperty: 'someOtherThing', 
    money: 2, 
    user: 'jane-doe'}
];

function sumByUser( input ) {
  // first aggregate the money per user
  const moneyByUser = input.reduce( (current, { user, money }) => {
    current[user] = (current[user] || 0) + money;
    return current;
  }, {} );
  
  // then create an array by using the keys for user names
  return Object
    .keys( moneyByUser )
    .map( user => ({ user, money: moneyByUser[user] }) );
}

console.log( sumByUser( input ) );

答案 1 :(得分:0)

您也可以使用下面的“ Array.reduce”和“ Array.from”方法

health check