Javascript 如何在另一个数组中包含一个数组的元素?

Javascript 如何在另一个数组中包含一个数组的元素?,javascript,html,angularjs,ionic-framework,Javascript,Html,Angularjs,Ionic Framework,例如,我有一个主数组,其中包含我需要的所有信息: $scope.songs = [ { title: 'Reggae', url:"#/app/mworkouts", id: 1 }, { title: 'Chill', url:"#/app/browse", id: 2 }, { title: 'Dubstep', url:"#/app/search", id: 3 }, { title: 'Indie', url:"#/app/sea

例如,我有一个主数组,其中包含我需要的所有信息:

  $scope.songs = [
    { title: 'Reggae',  url:"#/app/mworkouts", id: 1 },
    { title: 'Chill',   url:"#/app/browse",    id: 2 },
    { title: 'Dubstep', url:"#/app/search",    id: 3 },
    { title: 'Indie',   url:"#/app/search",    id: 4 },
    { title: 'Rap',     url:"#/app/mworkouts", id: 5 },
    { title: 'Cowbell', url:"#/app/mworkouts", id: 6 }
  ];
我只想将某些对象放入另一个数组中,而不需要输入每个对象,这样最终的结果看起来就像

  $scope.array1 = [
    { title: 'Reggae', url:"#/app/mworkouts",id: 1 },
    { title: 'Cowbell', url:"#/app/mworkouts",id: 6 }
  ];
我尝试过这个,但运气不好:

  $scope.array1 = [
    { $scope.songs[1] },
    { $scope.songs[6] }
  ];

我将不得不做一系列这样的工作,所以在每个对象中键入内容将花费很长时间,有没有更快的方法可以做到这一点?提前感谢:)

您需要移除大括号,因为它已经是一个对象。虽然数组索引从
0
开始,但是根据
0
更改索引值

$scope.array1 = [ 
   $scope.songs[0] ,
   $scope.songs[5] 
];

您需要这样做:

$scope.array1 = $scope.songs.filter(function (song) {
  return (song.title == "Reggae" || song.title == "Cowbell");
});
在这里,函数将为您提供一个经过过滤的新数组,以替换为原始范围值

或者,通过使用数组索引,您可以使用:

$scope.array1 = [ $scope.songs[0] , $scope.songs[5] ];

$scope.array1=[$scope.songs[1],$scope.songs[6]太棒了!很难不知道正确的语法…再次感谢!保持事物简单和性感,有没有一种方法可以改变对象中的一些信息,但保持所有其他信息不变?例如,仅将id:1更改为id:45,同时保持标题和url不变。@MehdiN是的,您可以这样做。。只需添加
song.id+=44。哈哈。可能会有不良影响。