简体   繁体   中英

How do I select single item from array using jQuery?

I have ID and description in array:

 var tablica = [{"3":"asdasd asd"},{"19":"asddas fff"},{"111111":"adas asf asdff ff"},{"4":"re"},{"5":"asdasd"},{"6":"we"},{"7":"asdasdgg"},{"9":"asdasdasd"},{"16":"sdads"},{"10":"asdgg"},{"11":"ggg"}];

How to select single description for ID using jQuery or JavaScript?

using es6,

 var tablica = [{"3":"asdasd asd"},{"19":"asddas fff"},{"3":"adas asf asdff ff"},{"4":"re"},{"5":"asdasd"},{"6":"we"},{"7":"asdasdgg"},{"9":"asdasdasd"},{"16":"sdads"},{"10":"asdgg"},{"11":"ggg"}]; var res = tablica.filter(x=> x["3"]) console.log(res); 

While you have duplicate keys in the objects, I suggest to use Array#filter for it and use later the object information, you need.

 var data = [{ 3: "asdasd asd" }, { 19: "asddas fff" }, { 3: "adas asf asdff ff" }, { 4: "re" }, { 5: "asdasd" }, { 6: "we" }, { 7: "asdasdgg" }, { 9: "asdasdasd" }, { 16: "sdads" }, { 10: "asdgg" }, { 11: "ggg" }], key = 3, result = data.filter(function (o) { return key in o; }); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

I think the best way is convert this array for object where the key for description will be ID. This eliminates the need to search every time when you want to find a description. Only once you prepare the object and later you have a simple and quick access to the description.

For example:

 var tablica = [{"3":"asdasd asd"},{"19":"asddas fff"},{"3":"adas asf asdff ff"},{"4":"re"},{"5":"asdasd"},{"6":"we"},{"7":"asdasdgg"},{"9":"asdasdasd"},{"16":"sdads"},{"10":"asdgg"},{"11":"ggg"}]; var obj = {}; tablica.forEach(function(el){ var id = Object.keys(el)[0]; obj[id] = el[id]; }); console.log(obj['5']); 

Now if you want to get description for ID = 5, you can easy access for it obj['5']

 $("button").eq(0).click(function() { $("h1").addClass("big-title"); })
 .big-title { font-size: 3rem; color: yellow; font-family: cursive; }
 <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>jQuery</title> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>Hello my people</h1> <button type="button" name="button">Click Me</button> <button type="button" name="button">Click Me</button> <button type="button" name="button">Click Me</button> <button type="button" name="button">Click Me</button> <button type="button" name="button">Click Me</button> <p>Click the first button</p> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="index.js" charset="utf-8"></script> </body> </html>

Well, it's a bit complicated since you have an array of objects

You could parse the whole array, and for each object in it, check if it has the wanted key:

 var tablica = [{"3":"asdasd asd"},{"19":"asddas fff"},{"3":"adas asf asdff ff"},{"4":"re"},{"5":"asdasd"},{"6":"we"},{"7":"asdasdgg"},{"9":"asdasdasd"},{"16":"sdads"},{"10":"asdgg"},{"11":"ggg"}]; var wantedId = "3"; // the ID you actually search var wantedObj; tablica.map(function(obj) { if (wantedId in obj) { wantedObj = obj; } }); console.log(wantedObj); 

Anyway, you should fix your data structure: each object of your array should have a property id and a property value (or something similar)

var tablica = [
    {
        id: "3",
        value: "asdasd asd"
    },
    {
        id: "19",
        value: "asddas fff"
    }
];

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