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?

2
  • 1
    Your indexOf is looking for 834 when it needs to look for {seat_number: 834} (the latter is not so easy given how equality checks don't work). Commented May 26, 2015 at 6:25
  • which framework are you using ?? Commented May 26, 2015 at 6:29

7 Answers 7

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
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

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

Comments

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
}

3 Comments

Note: This is an experimental technology, part of the ECMAScript 6 (Harmony) proposal. Check the compatibility table to see if .find will work on your browser.
@usandfriends, I agree. You can use Polyfill but only if you are using this type of comparison very often. Vote up for you.
yup. Here is the link to the polyfill.
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');
}

Comments

-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");
        }
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.