1

I am new to json. My json array

[{"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"}]

I want to find seat number already exist in array.

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

What is wrong?

Tharif
  • 13,794
  • 9
  • 55
  • 77
Piyush
  • 3,947
  • 9
  • 36
  • 69

7 Answers7

1

Note that ({a:10}) !== ({a:10}), therefore your indexOf won't work. See this related question.

Knowing that, there are several ways to do this (assuming typeof $scope.employeeData.new_seat_number === 'string'):

1) Use a for-loop:

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) Use .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');
}

Note: .some is the best answer other than the good old for-loop in my opinion. The rest are explorations of the Array API.

3) Use .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) Use .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');
}

Note: .find and .findIndex are experimental technologies. Check their compatibility tables to see if .find is available for your browser. If not, use their poly fills.

5) Use .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');
}

Note: Using .map will be much slower as .map iterates through the array running a function that will be used to create a brand new array.

Community
  • 1
  • 1
rgajrawala
  • 2,148
  • 1
  • 22
  • 35
1

You can do it the hard way if you want

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

I think that's a total pain tho. Once you have some reusable utility functions at your disposal, this problem becomes a lot easier to solve.

Don't freak out: the ES5 solution is below

// 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);

Now write some helpers to solve your task

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

Check it out

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

Here's the same code in 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);
  };
};

Your helpers

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

var hasSeat = comp(some)(eqSeat);

Try it out

console.log("831", hasSeat("834")(data)); // 831 true
console.log("8F3", hasSeat("8F3")(data)); // 8F3 true
console.log("999", hasSeat("999")(data)); // 999 false
Mulan
  • 129,518
  • 31
  • 228
  • 259
0
for(var i = 0; i < myjson.length; i++)
{
  if(myjson[i].seat_number == expectednumber)
  {
    alert('Seat number is already assigned.Please Check')
  }
}
jayprakashstar
  • 395
  • 2
  • 12
0

the item in your array is a object, so you can not use indexOf to find the seat_number.

you can try:

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');
    }
}
Yangguang
  • 1,785
  • 9
  • 10
0

Try this. Make use of filter(). If arr is empty, then it should show the 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');
}

You can also make use of 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');
}
Zee
  • 8,420
  • 5
  • 36
  • 58
0

You can use array find prototype. You may find detail on this method here.

Example:

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
}
Ranjan Kumar
  • 497
  • 2
  • 8
  • 24
  • 1
    Note: This is an __experimental__ technology, part of the ECMAScript 6 (Harmony) proposal. Check the [compatibility table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find#Browser_compatibility) to see if `.find` will work on your browser. – rgajrawala May 26 '15 at 06:39
  • @usandfriends, I agree. You can use Polyfill but only if you are using this type of comparison very often. Vote up for you. – Ranjan Kumar May 26 '15 at 06:45
  • yup. Here is the link to the [polyfill](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find#Polyfill). – rgajrawala May 26 '15 at 06:48
-1

Why do you need such a JSON Structure ?

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

Load your JSON using javascript/jquery

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");
        }
    }
});
Sony George
  • 558
  • 2
  • 8
  • 16