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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用