javascript - 使用 Javascript/AngularJS 将数组中的元素移动到开头

标签 javascript arrays angularjs filter

我正在开发一个 AngularJS 过滤器来选择电影,并且一直在寻找使用多个搜索词的方法。如果影片还没有被过滤器捕获,它就会到达数组的顶部。最终,我想按点击次数对数组进行排序,但与此同时,我只想对其进行设置,以便如果电影已在搜索结果数组中,则它会移至数组的顶部以便可以看到第一的。任何帮助将不胜感激!

// New module:

var app = angular.module('movieFinder', []);

// Search function:

app.filter('searchFor', function() {

  return function(arr, searchString) {

    // Show all movies until search begins

    if(!searchString) {
        return arr;
    }

    var result = [];

    // Search for each word. Change to lowercase. Remove any punctuation. Split into array.

    searchArray = searchString.toLowerCase().replace(/[^\w\s]|_/g, "").split(" ");

  // Work through each word.

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

    // Work through each movie.

    angular.forEach(arr, function(movie) {

      if((movie.title.toLowerCase().indexOf(searchArray[i]) !== -1) || 
        (movie.description.toLowerCase().indexOf(searchArray[i]) !== -1) || 
        (movie.director.toLowerCase().indexOf(searchArray[i]) !== -1) || 
        (movie.country.toLowerCase().indexOf(searchArray[i]) !== -1) || 
        (movie.year.toString().indexOf(searchArray[i]) !== -1) || 
        (movie.genre.toLowerCase().indexOf(searchArray[i]) !== -1) || 
        (movie.keywords.toLowerCase().indexOf(searchArray[i]) !== -1)) {

          // If the movie is not listed...

          if (result.indexOf(movie) === -1) {

            // Insert at top of array

            result.unshift(movie);

          };

        };
      });
    }

    return result;

  }

});

// The controller

function MovieFinderController($scope){

  // The data model

  $scope.movies = [

    {
      title: 'The Beat That My Heart Skipped',
      description: 'Will Thomas still lead a life of crime and cruelty, just like his thuggish father, or will he pursue his dream of becoming a pianist?',
      director: 'Jacques Audiard',
      country: 'France',
      year: 2005,
      genre: 'Noir Thriller',
      cover: 'images/thebeatthatmyheartskipped.jpg',
      keywords: 'piano, gangster, broody, thug'
    },{
      title: 'Chungking Express',
      description: 'Two melancholy Hong Kong policemen fall in love: one with a mysterious female underworld figure, the other with a beautiful and ethereal server at a late-night restaurant he frequents.',
      director: 'Wong Kar Wai',
      country: 'Hong Kong',
      year: 1994,
      genre: 'Romantic Comedy',
      cover: 'images/chungkingexpress.jpg',
      keywords: 'fast food, cops, music, wigs, dancing, Chinese food, heartache, eating'
    } 
(...)

感谢您的帮助!

最佳答案

使用Array.prototype.unshift将内容附加到数组的开头。

Array.prototype.unshift.apply(<your array>, <movies to be added at beginning>);

关于javascript - 使用 Javascript/AngularJS 将数组中的元素移动到开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37986622/

相关文章:

C 计算数组中的项目数

javascript - 带有动态列的 Angular 过滤表 ng-model 未设置

javascript - Angular 中的 Promise 方法

javascript - 如何将 json 数据值从 ng-repeat 更改为其他值?

javascript - 如何在 ShaderMaterial 上获得正确的阴影

c - 即使经过 6 个小时的编程,我也看不到程序输出的问题

javascript - 如何管理 Vue.js 复选框组件上的复杂状态行为?

参数名称 "int g[]"后面的 Java 方括号?

javascript - js函数用括号括起来,不调用括号

javascript - jQuery - 替换 MyBB 消息但不是其中的一部分