T
T
Tweak_stack2015-11-01 17:41:43
JavaScript
Tweak_stack, 2015-11-01 17:41:43

How to compare property values ​​of multiple objects?

have a JSON object

var jsonObject = JSON.parse('<?php echo $JSON_database ?>');

it has 3 objects in an array, with the same properties.
Object {id: "6", hierarchy: "0", login: "John", password: "Gregg"}
Object {id: "7", hierarchy: "0", login: "Hunner", password: "james"}

how to pull out all login properties (from 3 objects in the array (and if there are 1000 objects then pull out from 1000)), compare with the loginvalue variable obtained from html?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
Tweak_stack, 2015-11-01
@Tweak_stack

The matter is that I need to implement vsyazka, mysql -> php -> JSON -> JS -> checking -> login;
To carry out all the processing in the data in JS ....
I wonder if it is possible to pull properties from a set of objects for comparison.
For synchronous work with the database.

Y
Yuri, 2015-11-02
@kapitan7830

var arr = [
  {id: "6", hierarchy: "0", login: "John", password: "Gregg"},
  {id: "7", hierarchy: "0", login: "Hunner", password: "james"}
];

for (var i = 0; i < arr.length; i++) {
  if (arr[i].login == loginValue) {
    alert('Login found!');
    break;
  }
}

Only working with usernames and passwords on the client side is a bad idea

D
Denis, 2021-07-29
@Denisido

let arr = [
{"id_request" : "29", "st_name" : "В работе", "h_time" : "103"},
{"id_request" : "29", "st_name" : "В работе", "h_time" : "105"},
{"id_request" : "29", "st_name" : "Закрыта", "h_time" : "117"},
{"id_request" : "29", "st_name" : "Открыта", "h_time" : "101"}
];

let max = arr.reduce((acc, curr) => acc.h_time > curr.h_time ? acc : curr);
console.log(max);

let maxIndex = arr.reduce((acc, curr, i) => arr[acc].h_time > curr.h_time ? acc : i, 0);
console.log(maxIndex);

let max_history = request_history.reduce((acc, curr) => Math.floor(new Date(acc.h_time).getTime() / 1000) > Math.floor(new Date(curr.h_time).getTime() / 1000) ? acc : curr);


arr.sort((a, b) => parseFloat(b.h_time) - parseFloat(a.h_time));




let  added_events = ['103', '45', '567'];
let content = arr.filter(function(n) {
        return added_events.indexOf(n.h_time) !== -1;
      });
console.log(content);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question