If I passed array name as a string to a JS function, then how to loop through it inside the function?

November 23, 2016, at 3:55 PM

I have a large array of objects. I want to make an advanced search function that I call it like this:

var x = searcArray('students.id=5','history.date=2015/10/10',and more args);

Then the function will loop through students array and search for id=5 then loop through history inside that student and search for date=2015/10/10

then return that array or object

So how can I do it with JavaScript or JQuery?

Answer 1

In vanilla js you can use Array Filter or Array Find but exists a lot of libraries for collections as underscore.js

Answer 2

You can make use of keyword arguments which basically is an array and it holds all the args passed in the function:

function searcArray(){ 
  var arr = Array.prototype.map.call(arguments, function(item){ 
    return item.split('=')[1] 
  document.body.innerHTML = '<pre>'+ JSON.stringify(arr) + '</pre>'; 
searcArray('students.id=5', 'history.date=2015/10/10');

Answer 3

i tried like this, check here

window.students = {'id':5};
window.history = {'date':'2015/10/10'};
function searcArray(){
    var arr = arguments;
    var results = [];
    for(var i=0;i<arr.length;i++){
        var searchQuery = arr[i].split("=")[0];
        var searchTarget = arr[i].split("=")[1];
        var searchQueryArray = searchQuery.split(".")[0];
        var searchQueryIndex= searchQuery.split(".")[1];
        if(window[searchQueryArray][searchQueryIndex] == searchTarget){
    return results;
var x = searcArray('students.id=5','history.date=2015/10/10');
Rent Charter Buses Company
jQuery offset().top giving different values for IE

jQuery offset().top giving different values for IE

I am trying to create a fixed position header, but I can't do it in the normal pure CSS way as the site I am working one has the browser scroll disabled, with the full site in a scrollable div. .

Foundation Modal won&#39;t slide on reveal

Foundation Modal won't slide on reveal

I'm just getting started with Foundation and i'm currently trying to figure out how modals work. [nnn]I'm trying to implement one by using the exemple modals on a blank php page but i can't get it to slide like this one does.

Jquery Show/Hide of DIV not working

Jquery Show/Hide of DIV not working

I have a problem, which I hope can be solved. .

Global variable scope with event handler for dynamically created element

Global variable scope with event handler for dynamically created element

I want to change the behaviour of a Bootstrap dropdown based on a global state variable. If the uiState variable is 'normal' when it is clicked, it should display the dropdown, but if it's in another state it should do something else.