I need to find the key of last property starting with a string
JSON:
var data = {
"admin": "peterson",
"worker": "peter napier",
"housekeeper": "peterson",
"worker": "richard Ben",
"executive": "richard parker",
"executive": "peter alp",
"housekeeper": "richard johny",
"admin": "richardson"
};
I have to write an algorithm which will return the key corresponding to the last occurence of value starting with a string.
Ex: I need to get admin if I call findKey("richard")
I need to get executive if I call findKey("peter")
I have iterated the object using simple for loop as this
for (var key in yourobject) {
console.log(key, yourobject[key]);
}
But I like to know the fastest way of iterating this as my scenario has more than 100000 property.
Just iterate over your data and store each name beginning with your key :
function findkey(name) {
var lg = name.length,
found;
for(var line in data) {
if(data[line].length >= lg && data[line].substring(0,lg) === name) {
found = line;
}
}
return found;
}
Here you go
var findKey = function (string) {
var keyToReturn;
for(key in data){
if(data[key].indexOf(string) === 0)
keyToReturn = key;
}
return keyToReturn;
}
Related
I have a bunch of log data which is stored in a variable. Each log value contains a camera name and system ip. I want to create an object which has names as all the distinct system ip's and corresponding value as an array which contains all the camera names corresponding to that system ip. Below is my code ---
$http(req).success(function(data){
$scope.logs = data;
$scope.cameras={};
var v =$scope.logs[0].systemIp;
$scope.cameras["v"]=[];
$scope.cameras["v"].push($scope.logs[0].cameraName);
for(i=1;i<$scope.logs.length;i++){
v=$scope.logs[i].systemIp;
var flag=0;
for(j in $scope.cameras){
if(j==="v")
{
flag=1;
break;
}
}
if(flag==0)
{
$scope.cameras["j"]=[];
$scope.cameras["j"].push($scope.logs[i].cameraName);
}
else if(flag==1)
{
$scope.cameras["v"].push($scope.logs[i].cameraName);
}
}});
And this is what my data looks like --
[{
"_id": "57683fd82c77bb5a1a49a2aa",
"cameraIp": "192.16.0.9",
"cameraName": "garage2",
"systemIp": "192.168.0.2"
},
{
"_id": "57683f8e2c77bb5a1a49a2a9",
"cameraIp": "192.16.0.8",
"cameraName": "garage1",
"systemIp": "192.168.0.2"
},
{
"_id": "57683f5e2c77bb5a1a49a2a8",
"cameraIp": "192.16.0.7",
"cameraName": "Back Door",
"systemIp": "192.168.0.4"
}]
When I print $scope.cameras on my console it gives this as the output -
Object { v: Array[3] }
I want by cameras object to look like this --
{ "192.168.0.2" : [ "garage1" , "garage2"] ,
"192.168.0.4" : [ "Back Door"] }
I am new to javascript, any help is appreciated.
If you are using the Lodash or Underscore library (which I highly recommend), you can just use the _.groupBy() function to do what you are after (along with some other functions to ensure all values are unique).
However, you can also easily implement it yourself:
function groupByDistinct(arr, prop, mapFn) {
mapFn = mapFn || function (x) { return x; };
var output = {};
arr.forEach(function (item) {
var key = item[prop],
val = mapFn(item);
if (!output[key]) {
output[key] = [val];
return;
}
if (output[key].indexOf(val) < 0) {
output[key].push(val);
}
});
return output;
}
Use it for your code like so:
$scope.cameras = groupByDistinct(data, 'cameraIp', function (logEntry) {
return logEntry.cameraName;
});
You are passing a string such as "v" or "j" as your object key, and this string are actually ending being your object key and not the value of this variables as you want. You can use something like this:
for(i=0; i < $scope.logs.length; i++){
var _sysIp = $scope.logs[i].systemIp,
_camName = $scope.logs[i].cameraName;
if(!$scope.cameras.hasOwnProperty(_sysIp)) {
$scope.cameras[_sysIp] = [_camName];
} else if ($scope.cameras[_sysIp].indexOf(_camName) < 0) {
$scope.cameras[_sysIp].push(_camName);
}
}
I need your help,
Id' like to be able to come up with a javascript function that is similar to the following code structure below, except for the fact that I am not that strong enough in programming to come up with a workable solution to work from.
I'd like to be able to input a given value, then, using that value, search through an array and return the value short name (the value on the right side of the : colon character)
function test() {
var filenames = [
"REQUEST FOR INFO":"REQI",
"MEDIA CALL":"MC",
"ISSUES NOTE":"ISN"
]
EX1.)
var value_to_search_for = "REQUEST FOR INFO (ALPHA)"
if (value_to_search_for matches the value in the array filenames) then {
return "REQI"
}
EX.2)
var value_to_search_for = "MEDIA CALL"
if (value_to_search_for matches value in the array filenames) then {
return "MC"
}
}
You can change that to object and then you can do this
var filenames = {
"REQUEST FOR INFO": "REQI",
"MEDIA CALL": "MC",
"ISSUES NOTE": "ISN"
};
var getValue = function(val, obj) {
if (val in obj) return obj[val];
}
console.log(getValue('ISSUES NOTE', filenames));
You can also change that to array of objects and then you can do this
var filenames = [
{"REQUEST FOR INFO": "REQI"},
{"MEDIA CALL": "MC"},
{"ISSUES NOTE": "ISN"}
];
var getValue = function(val, array) {
array.forEach(function(el) {
for (prop in el) {
if (prop == val) console.log(el[prop]);
}
});
}
getValue('MEDIA CALL', filenames);
This question already has answers here:
JavaScript find json value [duplicate]
(5 answers)
Closed 7 years ago.
How to fine state name using postcode in bellow json data;
var data = '{
"1": {
"state": "VIC",
"postcode": "2600,2603,2605,2606"
},
"2": {
"state": "NSW",
"postcode": "2259,2264"
}
}'
How to find state by postcode;
if i search postcode 2600 if get result like VIC
Remove '' as yours is not a valid string, remove '' to make it a valid object literal, then you can iterate over the keys of the Object and check if it has the matching POSTCODE and if it has then return it's corresponding state.
var data = {
"1": {
"state": "VIC",
"postcode": "2600,2603,2605,2606"
},
"2": {
"state": "NSW",
"postcode": "2259,2264"
}
};
function getState(data, postcode){
for(var x in data){
if(data[x].postcode && data[x].postcode.split(",").indexOf(postcode.toString())!=-1) return data[x].state;
}
return "Not Found";
}
alert(getState(data, "2600"));
alert(getState(data, 2264));
You can directly do .indexOf on the postcode, even without using .split(","). But, then, it will also match with ,2600 which should not be the case. So, use split.
Use json[x].postcode condition to make sure that postcode field exists in the object. Otherwise, it will give an error if it does not exist.
Try like this
var data = '{"1": { "state": "VIC","postcode": "2600,2603,2605,2606"}, "2": {"state": "NSW","postcode": "2259,2264"}}';
var jsObj = JSON.parse(data);
var find = "2600";
var values = Object.keys(jsObj).filter(function(x) {
return jsObj[x].postcode.indexOf(find) > -1;
}).map(function(x) {
return jsObj[x].state;
});
console.log(values.length > 0 ? values[0] : "not found");
JSFIDDLE
function findState(data, postcode) {
var postcode = postcode.toString()
for (var k in data) {
var postcodes = data[k].postcode.split(",")
if (postcodes.indexOf(postcode) != -1)
return data[k].state
}
}
// Demo Output
var data = '{"1":{"state":"VIC","postcode":"2600,2603,2605,2606"},"2":{"state":"NSW","postcode":"2259,2264"}}'
var dataObj = JSON.parse(data)
var state = findState(dataObj, 2600)
document.write(state)
You can try something like this:
function searchInObject(object, searchKey, searchValue) {
for (var i in object) {
if (object[i][searchKey].indexOf(searchValue) > -1) {
return object[i];
}
}
}
(function() {
var data = {
"1": {
"state": "VIC",
"postcode": "2600,2603,2605,2606"
},
"2": {
"state": "NSW",
"postcode": "2259,2264"
}
}
var pin = "2600";
var result = searchInObject(data, "postcode", pin);
console.log(result.state);
pin = "2259";
result = searchInObject(data, "postcode", pin);
console.log(result.state);
})()
Well now.. You are just asking us to help you with homework :) Good thing im in a good mood.
First get a proper JSON string, and parse it into a object using JSON.parse. Then iterate this object and split the postcode string and find the state!
var data = ......
var resp = JSON.parse(data);
function getStateByPostcode(postcode) {
var state = "";
for(var i in resp) {
if(resp.hasOwnProperty(i)) {
var postcodes = resp[i]['postcode'].split(',');
if(postcodes.indexOf(postcode) !== -1) {
return resp[i]['state'];
}
}
}
return state;
}
Hello i want to filter json data like sql query without the help of plugins like alasql.js or linq.js or any plugins.
for example
{
"Managing PCL": [
{
"idItScreen": "1436",
"topicName": "Managing PCL",
"isFav": 0,
"cdeItScreen": "ListActiveTarif",
"busScreenName": "My Current Tarif"
},
{
"idItScreen": "1437",
"topicName": "Managing PCL",
"isFav": 0,
"cdeItScreen": "ListTermineTarif",
"busScreenName": "History Tarif"
}
]
}
for example i need to get data where idItScreen>1430 so that json data must be displayed the main challenge is to do without plugins so please reccomend me a good solution to do this without plugins
First turn your JSON into a Javascript object:
var obj = JSON.parse(myJSON);
Then do your filtering:
var matches = [];
var arr = obj['Managing PCL'];
for (var i = 0; i < arr.length; i++) {
if (arr[i].idItScreen > 1430) {
matches.push(arr[i]);
}
}
Or using jQuery.grep:
var matches = jQuery.grep(obj['Managing PCL'], function(n, i) {
return n.idItScreen > 1430;
});
Now matches contains the matching items.
If you want to get the JSON again, just use JSON.stringify:
var filteredJSON = JSON.stringify({'Managing PCL': matches});
You can also simply use .filter:
var matches = [];
var all = obj['Managing PCL'];
var filtered = all.filter(function(){
return $(this).idItScreen > 1430;
})
You don't need to use jQuery for this. You can use the filter() method of Array.prototype. See the working snippet below:
var obj = {
"Managing PCL": [{
"idItScreen": "1436",
"topicName": "Managing PCL",
"isFav": 0,
"cdeItScreen": "ListActiveTarif",
"busScreenName": "My Current Tarif"
}, {
"idItScreen": "1437",
"topicName": "Managing PCL",
"isFav": 0,
"cdeItScreen": "ListTermineTarif",
"busScreenName": "History Tarif"
}]
};
var filteredArray = obj['Managing PCL'].filter(function(item) {
return item.idItScreen > 1430;
});
obj['Managing PCL'] = filteredArray;
document.getElementById('result').innerHTML = JSON.stringify(obj);
<div id="result"></div>
You don't need jQuery for this. Use filter on the data instead.
function filterData(data, key, value) {
return data.filter(function (el) {
return el[key] > value;
});
}
// Note, `filter` operates on arrays, so you need to specify the
// array that contains the data
var result = filterData(data['Managing PCL'], 'idItScreen', '1430');
Also note that filter returns a new array containing the objects that it's found that match your criteria. You can access those objects in the usual way: result[0], for example.
DEMO
You could even expand this to create a function that returns data based on the operator too, not just greater-than, by using a look-up object:
var lookup = {
'>': function (data, value) { return data > value; },
'<': function (data, value) { return data < value; },
'===': function (data, value) { return data === value; }
}
function filterData(data, key, operator, value) {
return data.filter(function (el) {
return lookup[operator](el[key], value);
});
}
filterData(data['Managing PCL'], 'idItScreen', '>', '1430');
filterData(data['Managing PCL'], 'idItScreen', '===', '1430');
DEMO
I have an array like this:
var movies = [
{ Name: "The Red Violin", ReleaseYear: "1998", Director: "François Girard" },
{ Name: "Eyes Wide Shut", ReleaseYear: "1999", Director: "Stanley Kubrick" },
{ Name: "The Inheritance", ReleaseYear: "1976", Director: "Mauro Bolognini" }
];
I want to find the location of the movie that's released in 1999.
Should return 1.
What's the easiest way?
Thanks.
You will have to iterate through each value and check.
for(var i = 0; i < movies.length; i++) {
if (movies[i].ReleaseYear === "1999") {
// i is the index
}
}
Since JavaScript has recently added support for most common collection operations and this is clearly a filter operation on a collection, instead you could also do:
var moviesReleasedIn1999 = movies.filter(function(movie) {
return movie.ReleaseYear == "1999";
});
assuming you're not interested in the indexes but the actual data objects. Most people aren't anyways :)
.filter is not supported in all browsers, but you can add it yourself to your code base:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter#Compatibility
Built in? Use loops.
You want to get fancy? Linq to Javascript: http://jslinq.codeplex.com/
Something like:
function findMovieIndices(movies, prop, value) {
var result = [];
for(var i = movies.length; i--; ) {
if(movies[i][prop] === value) {
result.push(i); // personally I would return the movie objects
}
}
return result;
}
Usage:
var indices = findMovieIndices(movies, "ReleaseYear", "1999");
Maybe this gives you some idea for a more generalized function (if you need it).
Since you've also tagged it with jQuery, you could use the 'map' function:
var movies = $.map(movies,function(item,index){
return item.ReleaseYear == 1999 ? index : null;
});
This will return an array of indexes for all movies with the year of 1999. If you wanted the movies themselves as an array:
var movies = $.map(movies,function(item){
return item.ReleaseYear == 1999 ? item : null;
});
If functional style programming is applicable:
_.indexOf(_.pluck(movies, "ReleaseYear"), "1999")
Because it's that simple. The functional toolkit that is underscore.js can be very powerful.
_.indexOf , ._pluck
You'll have to create your own searching function.
Array.prototype.findMovieByYear = function (findYear) {
for (var i = 0; i < this.length; i++) {
// this actually returns the element, maybe you just want
// to return the array index ( the i param )
if (this[i].Release == findYear) return this[i];
}
return null;
// or return -1 or whatever if you want to return the index
};
// now you can call:
movies.findMovieByYear('1998');
// and that should return
{ Name: "The Red Violin", ReleaseYear: "1998", Director: "François Girard" }
Of course, this way of doing it actually affects every array you create.. which is maybe not what you want.. you can create your own array object then ...