在json数组中搜索

时间:2015-05-26 06:23:04

标签: javascript arrays json

我是json的新手。我的json数组

[{"seat_number":"834"},{"seat_number":"8F3"},{"seat_number":"891"},
{"seat_number":"814"},{"seat_number":"4081"},{"seat_number":"8F1"},
{"seat_number":"8F9"},{"seat_number":"4039"},{"seat_number":"0"},
{"seat_number":"509"},{"seat_number":"662"},{"seat_number":"561"},
{"seat_number":"791"},{"seat_number":"849"}]

我想在阵列中找到席位号。

if(($scope.employeeSeat.seat_array.indexOf($scope.employeeData.new_seat_number))>-1)
{
alert('Seat number is already assigned.Please Check');
} 

有什么问题?

7 个答案:

答案 0 :(得分:1)

请注意({a:10}) !== ({a:10}),因此您的indexOf将无效。请参阅this相关问题。

知道,有几种方法可以做到这一点(假设typeof $scope.employeeData.new_seat_number === 'string'):

1)使用for - 循环:

for (var i = 0; i < $scope.employeeSeat.seat_array.length; i++) {
    if ($scope.employeeSeat.seat_array[i].seat_number === $scope.employeeData.new_seat_number) {
        alert('Seat number is already assigned.Please Check');
    }
}

2)使用.some

if ($scope.employeeSeat.seat_array.some(function(seat) { return seat.seat_number === $scope.employeeData.new_seat_number; })) {
    alert('Seat number is already assigned.Please Check');
}

注意:.some是最好的答案,而不是我认为好的旧for循环。其余的是对Array API

的探索

3)使用.findIndex

if ($scope.employeeSeat.seat_array.findIndex(function(seat) { return seat.seat_number === $scope.employeeData.new_seat_number; }) !== -1) {
    alert('Seat number is already assigned.Please Check');
}

4)使用.find

if ($scope.employeeSeat.seat_array.find(function(seat) { return seat.seat_number === $scope.employeeData.new_seat_number; })) {
    alert('Seat number is already assigned.Please Check');
}

注意:.find.findIndex实验性技术。检查他们的compatibility tables,看看您的浏览器是否有.find。如果没有,请使用他们的poly fills

5)使用.map

if ($scope.employeeSeat.seat_array.map(function(seat) { return seat.seat_number; }).indexOf($scope.employeeData.new_seat_number)) !== -1) {
    alert('Seat number is already assigned.Please Check');
}

注意:使用.map更慢,因为.map遍历运行将用于创建全新数组的函数的数组。< / em>的

答案 1 :(得分:1)

如果你想要

,你可以这么做
var data = [
  {"seat_number":"834"},{"seat_number":"8F3"},
  {"seat_number":"891"},{"seat_number":"814"},
  {"seat_number":"4081"},{"seat_number":"8F1"},
  {"seat_number":"8F9"},{"seat_number":"4039"},
  {"seat_number":"0"},{"seat_number":"509"},
  {"seat_number":"662"},{"seat_number":"561"},
  {"seat_number":"791"},{"seat_number":"849"}
];

function matchingSeat(x) {
  return data.some(function(elem) {
    return elem.seat_number === x;
  });
}

console.log("831", matchingSeat("834")); // 834 true
console.log("8F3", matchingSeat("8F3")); // 8F3 true
console.log("999", matchingSeat("999")); // 999 false

我认为这是一个彻头彻尾的痛苦。一旦您拥有了一些可重复使用的实用程序功能,这个问题就变得容易解决了。

  

不要惊慌:ES5解决方案低于

// ES6
// reusable lib
let prop = y => x => x[y];

let comp = f => g => x => f(g(x));

let eq = y => x => x === y;

let some = f => xs => xs.some(f);

现在写一些助手来解决你的任务

let eqSeat = x => comp(eq(x))(prop("seat_number"));
let hasSeat = comp(some)(eqSeat);

检查出来

console.log("831", hasSeat("834")(data)); // 831 true
console.log("8F3", hasSeat("8F3")(data)); // 8F3 true
console.log("999", hasSeat("999")(data)); // 999 false

以下是 ES5

中的相同代码
// ES5
// reusable lib
var prop = function prop(y) {
  return function (x) {
    return x[y];
  };
};

var comp = function comp(f) {
  return function (g) {
    return function (x) {
      return f(g(x));
    };
  };
};

var eq = function eq(y) {
  return function (x) {
    return x === y;
  };
};

var some = function some(f) {
  return function (xs) {
    return xs.some(f);
  };
};

你的助手

var eqSeat = function eqSeat(x) {
  return comp(eq(x))(prop("seat_number"));
};

var hasSeat = comp(some)(eqSeat);

试一试

console.log("831", hasSeat("834")(data)); // 831 true
console.log("8F3", hasSeat("8F3")(data)); // 8F3 true
console.log("999", hasSeat("999")(data)); // 999 false

答案 2 :(得分:0)

for(var i = 0; i < myjson.length; i++)
{
  if(myjson[i].seat_number == expectednumber)
  {
    alert('Seat number is already assigned.Please Check')
  }
}

答案 3 :(得分:0)

数组中的项是一个对象,因此您无法使用indexOf来查找seat_number。

你可以尝试:

for(var i=0; i<$scope.employeeSeat.seat_array.length; ++i){
    if($scope.employeeData.new_seat_number == $scope.employeeSeat.seat_array[i].seat_number){
        alert('Seat number is already assigned.Please Check');
    }
}

答案 4 :(得分:0)

试试这个。利用filter()。如果arr为空,则应显示alert

var arr=$scope.employeeSeat.seat_array.filter(function(item){
  return $scope.employeeData.new_seat_number == item.seat_number;
})
if(!arr.length){
 alert('Seat number is already assigned.Please Check');
}

您还可以使用some()

var bool=$scope.employeeSeat.seat_array.some(function(item){
  return $scope.employeeData.new_seat_number == item.seat_number;
})
if(bool){
 alert('Seat number is already assigned.Please Check');
}

答案 5 :(得分:0)

您可以使用数组查找原型。您可以找到有关此方法的详细信息here

示例:

var items = [{"seat_number":"834"},{"seat_number":"8F3"},{"seat_number":"891"},{"seat_number":"814"},{"seat_number":"4081"},{"seat_number":"8F1"},{"seat_number":"8F9"},{"seat_number":"4039"},{"seat_number":"0"},{"seat_number":"509"},{"seat_number":"662"},{"seat_number":"561"},{"seat_number":"791"},{"seat_number":"849"}];

var newSeatNumber = $scope.employeeData.new_seat_number;
var result = items.find(function(item) {return item.seat_number == newSeatNumber ;})


console.log(result); // Object { seat_number="791"} if found, else undefined
if(result)
{
    //Do your stuff. Item exits in array
}

答案 6 :(得分:-1)

为什么需要这样的JSON结构?

I've changed the structure a little.
{
  "seat_numbers" :["894", "900", "814", "591", "789", ..]
}

使用javascript / jquery

加载您的JSON
var yourSearchingSeat = "900";
$.getJSON( "ajax/test.json", function( data ) {
    var seat_numbers = data.seat_numbers
    for (i=0; i<seat_numbers.length;i++) {
        if (seat_numbers[i] == yourSearchingSeat) {
            console.log("Seat number"+yourSearchingSeat+" already exists");
        }
    }
});