How can I get the index from a JSON object with value?
You will have to use Array.find
or Array.filter
or Array.forEach
.
Since your value is array and you need the position of the element, you will have to iterate over it.
Array.find
var data = [{"name":"placeHolder","section":"right"},{"name":"Overview","section":"left"},{"name":"ByFunction","section":"left"},{"name":"Time","section":"left"},{"name":"allFit","section":"left"},{"name":"allbMatches","section":"left"},{"name":"allOffers","section":"left"},{"name":"allInterests","section":"left"},{"name":"allResponses","section":"left"},{"name":"divChanged","section":"right"}];
var index = -1;
var val = "allInterests"
var filteredObj = data.find(function(item, i){
if(item.name === val){
index = i;
return i;
}
});
console.log(index, filteredObj);
How to write a select query to get the index value from Json object
If I understand the question correctly and you want to get the index of the items in the Object
JSON array, you need to use OPENJSON()
with default schema. The result is a table with columns key
, value
and type
and in case of JSON array, the key
column holds the index of each item in the array (0-based):
JSON:
DECLARE @json nvarchar(max) = N'{
"Model":[
{
"ModelName":"Test Model",
"Object":[
{
"ID":1,
"Name":"ABC"
},
{
"ID":11,
"Name":"ABCD"
},
{
"ID":15,
"Name":"ABCDE"
}
]
}
]
}'
Statement:
SELECT CONVERT(int, j2.[key]) + 1 AS item_id
FROM OPENJSON (@json, '$.Model') j1
CROSS APPLY OPENJSON(j1.[value], '$.Object') j2
But if you want to get the values of the ID
keys in the Object
JSON array, the statement is different:
SELECT j2.ID
FROM OPENJSON (@json, '$.Model') j1
CROSS APPLY OPENJSON(j1.[value], '$.Object') WITH (
ID int '$.ID'
) j2
Note, that you need two OPENJSON()
calls, because the input JSON has nested array structure. Of course, if Model
JSON array has always one item, you may simplify the statement using an appropriate path
:
SELECT CONVERT(int, [key]) + 1 AS item_id
FROM OPENJSON (@json, '$.Model[0].Object')
Finally, to get index
, ID
and Name
, you should use the following statement, which assumes, that $.Model
JSON array has more than one item and defines ID
and Name
columns with the appropraite data types:
SELECT
CONVERT(int, j2.[key]) + 1 AS ItemID,
j3.ID, j3.Name
FROM OPENJSON (@json, '$.Model') j1
CROSS APPLY OPENJSON(j1.[value], '$.Object') j2
CROSS APPLY OPENJSON(j2.[value], '$') WITH (
ID int '$.ID',
Name varchar(50) '$.Name'
) j3
get json key and value by index instead of their names
You can transform the values into an array with Object.values
, and then get the index you want:
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}')
console.log(obj.name) // John
var array = Object.values(obj)
console.log(array[0]) // John
how to get value from json object with its index number in reactjs like posts[2].title
Remove the map function and simply write
<li>
<span>{this.state.posts[0].title}</span>
</li>
Get the index of certain value of JSON Objects
if you want to get the index of the particular item by ID then first of all you have to get the item object by ID as below and then you can use indexOf to get the index of that particular item.
var item = yourArray.find(function (e) {
if (e.id == "your Id") {
return e;
}
});
var index = yourArray.indexOf(item);
How do I index an item in a JSON array from Python
Lets say you have your json in a dictionary format. If not you could use some library to convert to a dict (the json library does that)
then I would iterate over the dict and check if each element has the key, in the example "person2", if yes, we found the element and can print the index.
Here is the code:
i = 0
for person in json["users"]:
if "person2" in person:
print(i)
i += 1
There is probably a much cleaner solution, but that is what I can think of.
javascript json How to get index of key in json array
You can use ES2015 array method:
jsonArray.findIndex(item => Object.keys(item).includes("key3"));
Related Topics
Update Value in Localstorage Array by Existing Key
Make Discord Bot Send Picture With Message With Nodejs
Mongodb Get Data to Display in HTML
How to Close an Open Collapsed Navbar When Clicking Outside of the Navbar Element in Bootstrap 3
Check If a String Is HTML or Not
Whenever I Click Outside the Element It Should Close
Make a Clickable Link With Onclick But Without Href=#
Open a New Tab on Button Click in Angularjs
React Js - Uncaught Typeerror: This.Props.Data.Map Is Not a Function
React State Change Not Causing Re-Render
How to Get the Ajax Response from Success and Assign It in a Variable Using Jquery
How to Export Json Data to Excel File Using JavaScript
React Native: Image Not Displaying With Dynamic/Local Uri
Programmatically Clicking Button in React Native
How to Add Query String Parameter to Same Url When on Click a Href Link
How to Prevent the Keyboard from Popping Up on Mobile Devices