简体   繁体   中英

Equating JS variable to json encoded php array value regardless of index

I'm comparing a JS variable to a json encoded php array value, which is working currently as is:

function myFunction() {
    var itemArray = <?php echo json_encode($items);?>;   
    console.log(itemArray);

    var number = 14;

    if(number == itemArray[0]['number']){
        success.style.display = "block";
        error.style.display = "none";
    }
}

[{
    0:
        name: "nameOne"
        number: 14
    1:
        name: "nameTwo"
        number: 13
    2:
        name: "nameThree"
        number: 8
}]

$items: array:3 [
    0=>array{
        name: "nameOne"
        number: 14
    }
    1=>array{
        name: "nameTwo"
        number: 13

    }
    2=>array{
        name: "nameThree"
        number: 8
    }
]

So It's indexed to zero which is fine for testing the comparison, but this isn't what I want. I want to be able to look at the 'number' field of every index in this object. So basically I want to say "If 14 exists as 'Number' in any of the indeces here, then this should be true".

I want to do this for other fields eventually too, but when I change my index to if(number == itemArray[]['number']){ then it says the function is undefined.

How can I do this for any index in the object to look at that field?

If you just want to array an array of objects that match the key try this. The 2nd function returns an array of the matching indices, although there is probably a better way to do that.

 var array = [{ name: "nameOne", number: 14 },{ name: "nameTwo", number: 13 },{ name: "nameThree", number: 8 },{ name: "nameThree", number: 14 } ]; function findMatches(array, key, value) { return array.filter(item => item[key] == value); } function findIndex(array, key, value) { return array.map((item,index) => item[key] === value ? index : undefined).filter(x => x !== undefined); } console.log(findMatches(array, "number", 14)); console.log(findIndex(array, "number", 14)); console.log(findMatches(array, "number", 13)); console.log(findIndex(array, "number", 13)); console.log(findMatches(array, "number", 2)); console.log(findIndex(array, "number", 2)); 

<?php
//I suppose Your items will be like
$items = 
array(array("name" => "nameOne","number" => 14),
    array("name"=> "nameTwo","number"=> 13),
     array("name"=> "nameThree","number"=> 8)
);

echo "<pre>";
print_r($items);
?>

<script>
myFunction();
function myFunction() {
  //Use the JavaScript function JSON.parse() to convert text into a JavaScript object:
   var json  = JSON.parse('<?php echo json_encode($items);?>');   
   console.log(json );

   var number = 14;
   var result = false;
   var error = '';
   //Use the loop to get the value from json
  for(var i = 0; i < json.length; i++) {
    var obj = json[i];

   //If obj.number is 14 add your code
   if(number == obj.number){
      result = true;

   }
  }

 if(result){
    alert('success message');
    success.style.display = "block";
    error.style.display = "none";
 }else{
    success.style.display = "none";
    error.style.display = "block";
 }

}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM