简体   繁体   English

如何获取JSON数组中的第一个密钥对?

[英]How to get the first key pair in array of JSON?

Currently, I have my JSON and a function that loops through the results and then through the URLs. 目前,我有我的JSON和一个循环结果然后通过URL的函数。 I am trying to only get the first type value which is detail. 我试图只获得第一个类型的值,这是详细的。 I have tried looking for ways to get the first value and found that using [0] can work in some situations but not this one. 我试图找到获得第一个值的方法,并发现使用[0]可以在某些情况下工作,但不是这个。 Am I indexing incorrectly? 我索引不正确吗? and is there a more succinct way of coding this nested for loop? 并且有一种更简洁的方法来编译这个嵌套的循环吗?

 const data = { "data": { "results": [{ "name": "Deadpool", "urls": [{ "type": "detail", "url": "http://marvel.com/characters/12/deadpool?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" }, { "type": "wiki", "url": "http://marvel.com/universe/Deadpool_(Wade_Wilson)?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" }, { "type": "comiclink", "url": "http://marvel.com/comics/characters/1009268/deadpool?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" } ] }] } }; function test(data) { const dataArr = data.data['results']; for (let i = 0; i < dataArr.length; i++) { console.log(dataArr[i].urls); const urlArr = dataArr[i].urls for (let j = 0; j < urlArr.length; j++) { console.log(urlArr[j].type[0]); } } } test(data); 

A functional approach... 功能性方法......

Your question seems a little vague, but I think you're looking for the value of type from the first item in each urls array. 你的问题似乎有点模糊,但我认为你正在寻找每个urls数组中第一项的type值。

 const data = { "data": { "results": [{ "name": "Deadpool", "urls": [{ "type": "detail", "url": "http://marvel.com/characters/12/deadpool?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" }, { "type": "wiki", "url": "http://marvel.com/universe/Deadpool_(Wade_Wilson)?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" }, { "type": "comiclink", "url": "http://marvel.com/comics/characters/1009268/deadpool?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" }] }] } }; const types = data.data.results.map(({ urls: [first, ...rest] }) => first.type); console.log(types); 


Let's break this down... 让我们打破这个......

data.data.results.map(...)

Array.prototype.map will return a new array with the results of calling the provided function on every element in the results array. Array.prototype.map将返回一个新数组,其结果是在results数组中的每个元素上调用提供的函数。

({ urls: ... })

This is called destructuring . 这称为解构 It defines a new variable, urls , that is assigned the value of the urls property as we iterate through the results items. 它定义了一个新变量urls ,当我们遍历results项时,它会分配urls属性的值。

({ urls: [first, ...rest] })

The value of the urls variable we defined will be an array. 我们定义的urls变量的值将是一个数组。 We only care about the first item in the array, so we'll spread the array, defining a new variable called first that's assigned the value of the first item, and a variable, rest , that will be an array of the rest of the items. 我们只关心数组中的第一个项,所以我们将扩展数组,定义一个名为first的新变量,它赋予第一个项的值,以及一个变量rest ,它将是其余部分的数组。项目。 Basically, take the head of the array. 基本上,采取阵列的头部。 These operations are called spread and rest , respectively. 这些操作分别称为传播休息

({  urls: [first, ...rest] }) => first.type

Finally, return the value of the type property from the first urls item. 最后,从第一个urls项返回type属性的值。


If I'm completely wrong 如果我完全错了

And you want the "details" item within each urls array, then a slight change will suffice: 并且您想要每个urls数组中的“details”项,然后稍微更改就足够了:

 const data = { "data": { "results": [{ "name": "Deadpool", "urls": [{ "type": "detail", "url": "http://marvel.com/characters/12/deadpool?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" }, { "type": "wiki", "url": "http://marvel.com/universe/Deadpool_(Wade_Wilson)?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" }, { "type": "comiclink", "url": "http://marvel.com/comics/characters/1009268/deadpool?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" }] }] } }; const urls = data.data.results.map(({ urls }) => urls.find(({ type }) => type === 'detail') ); console.log(urls); 


If you'd like to learn more about functional programming, Eric Elliott wrote an excellent set of articles on functional programming in JavaScript. 如果您想了解有关函数式编程的更多信息,Eric Elliott撰写了一系列关于JavaScript函数式编程的优秀文章。 Here's the first one . 这是第一个

You can use find to find the exact element by type prop. 您可以使用find通过type prop找到确切的元素。

 const data = { "data": { "results": [{ "name": "Deadpool", "urls": [{ "type": "detail", "url": "http://marvel.com/characters/12/deadpool?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" }, { "type": "wiki", "url": "http://marvel.com/universe/Deadpool_(Wade_Wilson)?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" }, { "type": "comiclink", "url": "http://marvel.com/comics/characters/1009268/deadpool?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" } ] }] } }; function test(data) { const dataArr = data.data['results']; for (let i = 0; i < dataArr.length; i++) { //console.log(dataArr[i].urls); const urlArr = dataArr[i].urls const detail = urlArr.find(url => url.type == 'detail'); console.log(detail.url); } } test(data); 

If the format of your data is fixed you can use. 如果您的数据格式是固定的,您可以使用。 You can also try different approaches of safe reading data from object. 您还可以尝试从对象安全读取数据的不同方法。 You can use utilities like these : 您可以像使用公用事业这些

data.data['results'][0].urls[0]

If you are not sure about the 0th index you can use find : 如果您不确定第0个索引,可以使用find

data.data['results'][0].urls.find(url => url.type === 'detail')

Try this- 尝试这个-

function test(data) 
{
    var dataArr = data.data.results;

    for (let i = 0; i < dataArr.length; i++) {

        const urlArr = dataArr[i].urls
        for (let j = 0; j < urlArr.length; j++) {
            console.log(urlArr[0].type);
        }
    }

}

Something like this? 像这样的东西?

 const data = { "data": { "results": [{ "name": "Deadpool", "urls": [{ "type": "detail", "url": "http://marvel.com/characters/12/deadpool?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" }, { "type": "wiki", "url": "http://marvel.com/universe/Deadpool_(Wade_Wilson)?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" }, { "type": "comiclink", "url": "http://marvel.com/comics/characters/1009268/deadpool?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" } ] }] } }; const result = [].concat.apply([], data.data.results.map(x => x.urls.map(y => y.type)))[0]; console.log(result); 

 const data = { "data": { "results": [{ "name": "Deadpool", "urls": [{ "type": "detail", "url": "http://marvel.com/characters/12/deadpool?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" }, { "type": "wiki", "url": "http://marvel.com/universe/Deadpool_(Wade_Wilson)?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" }, { "type": "comiclink", "url": "http://marvel.com/comics/characters/1009268/deadpool?utm_campaign=apiRef&utm_source=6fa9bcf637a9185ee2e3035cb2d3b465" } ] }] } }; console.log(data.data.results.map(el => el.urls).flat().find(el => el.type === 'detail')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM