duanjiu1894 2015-03-08 04:40
浏览 84
已采纳

基于Javascript中的子数组对象重新分类父数组

(Also open to a php solution)

If I have an array within an array, how do you re-categorize the parent array based on the child array's objects?

Here is an example of what I would like to achieve:

Say if the original array is:

[
 {
  "name":"Jade",
  "tags":[
   "developer"
  ]
 },
 {
  "name":"Bridge",
  "tags":[
   "cook"
  ]
 },
 {
  "name":"Ever",
  "tags":[
   "artist"
  ]
 },
 {
  "name":"Noah",
  "tags":[
   "artist",
   "developer"
  ]
 }
]

I want to be able to re-categorize it into:

[
 "artist": {
    {
     {
      "name":"Noah",
      "tags":[
       "artist",
       "developer"
      ]
    }
  }
 "developer": {
   {
      "name":"Jade",
      "tags":[
       "developer"
      ]
      },
     {
      "name":"Noah",
      "tags":[
       "artist",
       "developer"
      ]
    }
  },
  "cook": {
    {
      "name":"Bridge",
      "tags":[
       "cook"
      ]
    },
  }
]

Here is what I tried:

(the original array is var data)

var data = [original array]
var newArray = [];

for (var i = 0; i < data.length; i++) {
  for (var a = 0; a < data[i].tags.length; a++) {
    newArray[] = data[i].tags[a];
    if (newArray[i] == data[i].tags[a]) {
      newArray[i] = data[i];
    }
  }
}

My theory is to pop the tag (in tags) into a new array, and if the original object.tags has same value as the new object in the array, pop the original object into the new object.

This approach failed.

  • 写回答

1条回答 默认 最新

  • dsiv4041 2015-03-08 05:19
    关注

    I'd recommend a backend solution (php) as it will be significantly faster as you scale up.

    <?php
        $sInFile  = 'in.json';
        $sOutFile = 'out.json';
    
        $aDevelopers = array();
        $aArtists    = array();
        $aCooks      = array();
    
        $sRaw       = file_get_contents( $sInFile );
        $aInData    = json_decode( $sRaw );
        $iCountData = count( $aInData );
        for( $i = 0; $i < $iCountData; ++$i )
        {
            $iCountTags = count( $aInData[ $i ]->tags );
            for( $j = 0; $j < $iCountTags; ++$j )
            {
                $aTmp = array();
                if( ( !empty( $aInData[ $i ]->tags[ $j ] ) ) && $aInData[ $i ]->tags[ $j ] == 'developer' )
                {
                    $aTmp[ 'name' ] = $aInData[ $i ]->name;
                    $aTmp[ 'tags' ] = $aInData[ $i ]->tags;
                    $aDevelopers[] = $aTmp;
                }
                $aTmp = array();
                if( ( !empty( $aInData[ $i ]->tags[ $j ] ) ) && $aInData[ $i ]->tags[ $j ] == 'artist' )
                {
                    $aTmp[ 'name' ] = $aInData[ $i ]->name;
                    $aTmp[ 'tags' ] = $aInData[ $i ]->tags;
                    $aArtists[] = $aTmp;
                }
                $aTmp = array();
                if( ( !empty( $aInData[ $i ]->tags[ $j ] ) ) && $aInData[ $i ]->tags[ $j ] == 'cook' )
                {
                    $aTmp[ 'name' ] = $aInData[ $i ]->name;
                    $aTmp[ 'tags' ] = $aInData[ $i ]->tags;
                    $aCooks[] = $aTmp;
                }
            }
        }
    
        $aFull[ 'developer'] = $aDevelopers;
        $aFull[ 'artist' ] = $aArtists;
        $aFull[ 'cook' ] = $aCooks;
        $sFull = json_encode( $aFull );
        file_put_contents( $sOutFile, $sFull );
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line