-1

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?

GSMX
  • 95
  • 3
  • 8

5 Answers5

1

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);
A.T.
  • 24,694
  • 8
  • 47
  • 65
  • First, please do not answer duplicates. Second, your solution will fail for cases when value of `x["3"]` is falsey. – Rajesh Jan 13 '17 at 10:34
  • @Rajesh OP said "I have ID and description in array", so clearly description in value, so I can assume that there will be very very rare chance of false in description. secondly if u really think it is duplicate then please mark this question with duplicate question of which it is duplication, that you didn't clearly. – A.T. Jan 15 '17 at 15:00
  • @Rajesh same I feel with your comment, please let me know if there is any dup. question, I will mark it for you – A.T. Jan 15 '17 at 15:05
1

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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Please do not answer duplicates.I know its not exact duplicate, but if OP is considering `3` as ID, its better to suggest using better data structure `{'id': 3, value:...}` and then it becomes exact duplicate. – Rajesh Jan 13 '17 at 10:34
1

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']

Piotr Białek
  • 2,569
  • 1
  • 17
  • 26
  • Please note, you should not answer duplicates. – Rajesh Jan 13 '17 at 10:35
  • Please refer comments under question or on Nina's answer – Rajesh Jan 13 '17 at 10:37
  • Lets not debate about it. Also please form better sentence as didn't understand your previous comment. And its was about my **comment on her answer** and not about her answer. And finally, your solution will give value in last occurrence of object and not all values. – Rajesh Jan 13 '17 at 10:45
0

$("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>
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 13 '21 at 07:49
-1

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"
    }
];
pistou
  • 2,799
  • 5
  • 33
  • 60
  • 1
    First, using `.map` without `return` is just wasting a functionality. Second, I'd rather use `filter` or `find` as searching is what they are intended for. Third, you **should not** answer a duplicate question. – Rajesh Jan 13 '17 at 10:14