运营商未按预期工作的下划线

时间:2018-03-27 23:15:43

标签: javascript underscore.js where

这是album.tracks.items

的json内容
[{
        "disc_number": 1,
        "duration_ms": 220613,
        "explicit": false,
        "external_urls": {
            "spotify": "https://open.spotify.com/track/5dcQ8zP4ruXLGvxvnaqiIR"
        },
        "href": "https://api.spotify.com/v1/tracks/5dcQ8zP4ruXLGvxvnaqiIR",
        "id": "5dcQ8zP4ruXLGvxvnaqiIR",
        "is_local": false,
        "name": "It Ain't Love (feat. Lil Mo)",
        "preview_url": "https://p.scdn.co/mp3-preview/b0b7c4631088cf6c358f3fd2f368621064c81169?cid=774b29d4f13844c495f206cafdad9c86",
        "track_number": 1,
        "type": "track",
        "uri": "spotify:track:5dcQ8zP4ruXLGvxvnaqiIR",
        "url_path": "http://localhost:5000/music/4tnnrgXBrdF0b4q3lZqL0v/1/1"
    },
    {
        "disc_number": 1,
        "duration_ms": 206173,
        "explicit": false,
        "external_urls": {
            "spotify": "https://open.spotify.com/track/3skhDoiKAW73vulWCRsWjW"
        },
        "href": "https://api.spotify.com/v1/tracks/3skhDoiKAW73vulWCRsWjW",
        "id": "3skhDoiKAW73vulWCRsWjW",
        "is_local": false,
        "name": "1000 Cigarettes",
        "preview_url": "https://p.scdn.co/mp3-preview/157a6131e35c16519ef2a2b306523fc5c5f54dc0?cid=774b29d4f13844c495f206cafdad9c86",
        "track_number": 2,
        "type": "track",
        "uri": "spotify:track:3skhDoiKAW73vulWCRsWjW",
        "url_path": "http://localhost:5000/music/4tnnrgXBrdF0b4q3lZqL0v/1/2"
    },
    {
        "disc_number": 1,
        "duration_ms": 171226,
        "explicit": false,
        "external_urls": {
            "spotify": "https://open.spotify.com/track/0u33BbRnsU2TCZyJaQ1NH3"
        },
        "href": "https://api.spotify.com/v1/tracks/0u33BbRnsU2TCZyJaQ1NH3",
        "id": "0u33BbRnsU2TCZyJaQ1NH3",
        "is_local": false,
        "name": "Bounce (feat. Nore and Isis)",
        "preview_url": "https://p.scdn.co/mp3-preview/2379a2e8849111c4a2faa1abddf273ba51711584?cid=774b29d4f13844c495f206cafdad9c86",
        "track_number": 3,
        "type": "track",
        "uri": "spotify:track:0u33BbRnsU2TCZyJaQ1NH3",
        "url_path": "http://localhost:5000/music/4tnnrgXBrdF0b4q3lZqL0v/1/3"
    },
    {
        "disc_number": 1,
        "duration_ms": 219333,
        "explicit": false,
        "external_urls": {
            "spotify": "https://open.spotify.com/track/2REmc7SO87Ex6MtPQPafLl"
        },
        "href": "https://api.spotify.com/v1/tracks/2REmc7SO87Ex6MtPQPafLl",
        "id": "2REmc7SO87Ex6MtPQPafLl",
        "is_local": false,
        "name": "Vuvuvu",
        "preview_url": "https://p.scdn.co/mp3-preview/c996b13cf701175ce75d2a1ba4f1f8d54ff1b090?cid=774b29d4f13844c495f206cafdad9c86",
        "track_number": 4,
        "type": "track",
        "uri": "spotify:track:2REmc7SO87Ex6MtPQPafLl",
        "url_path": "http://localhost:5000/music/4tnnrgXBrdF0b4q3lZqL0v/1/4"
    },
    {
        "disc_number": 1,
        "duration_ms": 192853,
        "explicit": false,
        "external_urls": {
            "spotify": "https://open.spotify.com/track/7d5Rp5q6XUegGhrIxEXja4"
        },
        "href": "https://api.spotify.com/v1/tracks/7d5Rp5q6XUegGhrIxEXja4",
        "id": "7d5Rp5q6XUegGhrIxEXja4",
        "is_local": false,
        "name": "Heartbreaker",
        "preview_url": "https://p.scdn.co/mp3-preview/5f280fbebded1b2ce5320e43f75ac32628ccae00?cid=774b29d4f13844c495f206cafdad9c86",
        "track_number": 5,
        "type": "track",
        "uri": "spotify:track:7d5Rp5q6XUegGhrIxEXja4",
        "url_path": "http://localhost:5000/music/4tnnrgXBrdF0b4q3lZqL0v/1/5"
    },
    {
        "disc_number": 1,
        "duration_ms": 243840,
        "explicit": false,
        "external_urls": {
            "spotify": "https://open.spotify.com/track/07r3G29bZvODU5Lr0Nk6IZ"
        },
        "href": "https://api.spotify.com/v1/tracks/07r3G29bZvODU5Lr0Nk6IZ",
        "id": "07r3G29bZvODU5Lr0Nk6IZ",
        "is_local": false,
        "name": "Fist Of God",
        "preview_url": "https://p.scdn.co/mp3-preview/9453101e1568b713b0af6c1da4cfabd5d6664f03?cid=774b29d4f13844c495f206cafdad9c86",
        "track_number": 6,
        "type": "track",
        "uri": "spotify:track:07r3G29bZvODU5Lr0Nk6IZ",
        "url_path": "http://localhost:5000/music/4tnnrgXBrdF0b4q3lZqL0v/1/6"
    },
    {
        "disc_number": 1,
        "duration_ms": 206880,
        "explicit": false,
        "external_urls": {
            "spotify": "https://open.spotify.com/track/66ZmwiF97Zk11pz5Jyfbwr"
        },
        "href": "https://api.spotify.com/v1/tracks/66ZmwiF97Zk11pz5Jyfbwr",
        "id": "66ZmwiF97Zk11pz5Jyfbwr",
        "is_local": false,
        "name": "So Deep (feat. Jahmal of The Carps)",
        "preview_url": "https://p.scdn.co/mp3-preview/6b62f7c210d42b266ac39064d90adf842db33465?cid=774b29d4f13844c495f206cafdad9c86",
        "track_number": 7,
        "type": "track",
        "uri": "spotify:track:66ZmwiF97Zk11pz5Jyfbwr",
        "url_path": "http://localhost:5000/music/4tnnrgXBrdF0b4q3lZqL0v/1/7"
    },
    {
        "disc_number": 1,
        "duration_ms": 208133,
        "explicit": false,
        "external_urls": {
            "spotify": "https://open.spotify.com/track/4syNWEL0JzzybCfZ9MZHPv"
        },
        "href": "https://api.spotify.com/v1/tracks/4syNWEL0JzzybCfZ9MZHPv",
        "id": "4syNWEL0JzzybCfZ9MZHPv",
        "is_local": false,
        "name": "Click Click (feat. E-40)",
        "preview_url": "https://p.scdn.co/mp3-preview/62b6d3becb7fdabf6d0d706cf2ef273bdc477754?cid=774b29d4f13844c495f206cafdad9c86",
        "track_number": 8,
        "type": "track",
        "uri": "spotify:track:4syNWEL0JzzybCfZ9MZHPv",
        "url_path": "http://localhost:5000/music/4tnnrgXBrdF0b4q3lZqL0v/1/8"
    },
    {
        "disc_number": 1,
        "duration_ms": 231040,
        "explicit": false,
        "external_urls": {
            "spotify": "https://open.spotify.com/track/5oBKd1jkmrZ6h9FbvdIzDS"
        },
        "href": "https://api.spotify.com/v1/tracks/5oBKd1jkmrZ6h9FbvdIzDS",
        "id": "5oBKd1jkmrZ6h9FbvdIzDS",
        "is_local": false,
        "name": "Word Up (feat. Ghostface Killah)",
        "preview_url": "https://p.scdn.co/mp3-preview/20d1b61b80ded729248f58cfbab178f92b17004a?cid=774b29d4f13844c495f206cafdad9c86",
        "track_number": 9,
        "type": "track",
        "uri": "spotify:track:5oBKd1jkmrZ6h9FbvdIzDS",
        "url_path": "http://localhost:5000/music/4tnnrgXBrdF0b4q3lZqL0v/1/9"
    },
    {
        "disc_number": 1,
        "duration_ms": 218453,
        "explicit": false,
        "external_urls": {
            "spotify": "https://open.spotify.com/track/6RsIWxNzE82KdYztIT1IK1"
        },
        "href": "https://api.spotify.com/v1/tracks/6RsIWxNzE82KdYztIT1IK1",
        "id": "6RsIWxNzE82KdYztIT1IK1",
        "is_local": false,
        "name": "Breakaway (feat. Jahmal of The Carps)",
        "preview_url": "https://p.scdn.co/mp3-preview/8ea8912ae9bad9936090cee3ad85614142782d2c?cid=774b29d4f13844c495f206cafdad9c86",
        "track_number": 10,
        "type": "track",
        "uri": "spotify:track:6RsIWxNzE82KdYztIT1IK1",
        "url_path": "http://localhost:5000/music/4tnnrgXBrdF0b4q3lZqL0v/1/10"
    },
    {
        "disc_number": 1,
        "duration_ms": 191560,
        "explicit": false,
        "external_urls": {
            "spotify": "https://open.spotify.com/track/2RYqwivWy9seoylZt6qUmH"
        },
        "href": "https://api.spotify.com/v1/tracks/2RYqwivWy9seoylZt6qUmH",
        "id": "2RYqwivWy9seoylZt6qUmH",
        "is_local": false,
        "name": "1000 Cigarettes (feat. Freeway)",
        "preview_url": "https://p.scdn.co/mp3-preview/0d38c708b8605d60fdaa386c790fe3a7be4cb0e1?cid=774b29d4f13844c495f206cafdad9c86",
        "track_number": 11,
        "type": "track",
        "uri": "spotify:track:2RYqwivWy9seoylZt6qUmH",
        "url_path": "http://localhost:5000/music/4tnnrgXBrdF0b4q3lZqL0v/1/11"
    }
]

我正在尝试根据disc_number为1且track_number为4(第四个元素)的两个条件在上面的json数组中获取元素 req.params.disc_number: 1, req.params.track_number: 4

var _ = require('underscore');
...
var track = _.where(album.tracks.items, {disc_number: req.params.disc_number, track_number: req.params.track_number});
console.log('track: ' + track)

track什么都不返回([console] track:),但它应该返回第四个元素(这个):

{ disc_number: 1,
duration_ms: 293600,
explicit: false,
external_urls:
{ spotify: 'https://open.spotify.com/track/616NBqU3gbjICFB1npf2vg' },
href: 'https://api.spotify.com/v1/tracks/616NBqU3gbjICFB1npf2vg',
id: '616NBqU3gbjICFB1npf2vg',
is_local: false,
name: 'Twilight Galaxy',
preview_url: null,
track_number: 4,
type: 'track',
uri: 'spotify:track:616NBqU3gbjICFB1npf2vg'}

当我通过parseInt()disc_number时,我认为我不需要track_number_where运算符曾经在我有一个条件之前工作

不确定什么是错的

编辑1:我修复了从代码中复制json的错误(现在它有效),但它仍无法正常工作

1 个答案:

答案 0 :(得分:2)

var data = [ { disc_number: 1,
     duration_ms: 286013,
     explicit: false,
     external_urls:
      { spotify: 'https://open.spotify.com/track/0cahtHEywK64NSWqm2SDTi' },
     href: 'https://api.spotify.com/v1/tracks/0cahtHEywK64NSWqm2SDTi',
     id: '0cahtHEywK64NSWqm2SDTi',
     is_local: false,
     name: 'Help I\'m Alive',
     preview_url: null,
     track_number: 1,
     type: 'track',
     uri: 'spotify:track:0cahtHEywK64NSWqm2SDTi'},
   { disc_number: 1,
     duration_ms: 257413,
     explicit: false,
     external_urls:
      { spotify: 'https://open.spotify.com/track/2uxLjFYEoQ7xsqfMPuwTQ9' },
     href: 'https://api.spotify.com/v1/tracks/2uxLjFYEoQ7xsqfMPuwTQ9',
     id: '2uxLjFYEoQ7xsqfMPuwTQ9',
     is_local: false,
     name: 'Sick Muse',
     preview_url: null,
     track_number: 2,
     type: 'track',
     uri: 'spotify:track:2uxLjFYEoQ7xsqfMPuwTQ9'}, 
   { disc_number: 1,
     duration_ms: 222933,
     explicit: true,
     external_urls:
      { spotify: 'https://open.spotify.com/track/5lVXRLV1LgQgd7kUGt3Mae' },
     href: 'https://api.spotify.com/v1/tracks/5lVXRLV1LgQgd7kUGt3Mae',
     id: '5lVXRLV1LgQgd7kUGt3Mae',
     is_local: false,
     name: 'Satellite Mind',
     preview_url: null,
     track_number: 3,
     type: 'track',
     uri: 'spotify:track:5lVXRLV1LgQgd7kUGt3Mae'},
   { disc_number: 1,
     duration_ms: 293600,
     explicit: false,
     external_urls:
      { spotify: 'https://open.spotify.com/track/616NBqU3gbjICFB1npf2vg' },
     href: 'https://api.spotify.com/v1/tracks/616NBqU3gbjICFB1npf2vg',
     id: '616NBqU3gbjICFB1npf2vg',
     is_local: false,
     name: 'Twilight Galaxy',
     preview_url: null,
     track_number: 4,
     type: 'track',
     uri: 'spotify:track:616NBqU3gbjICFB1npf2vg'},
   { disc_number: 1,
     duration_ms: 245280,
     explicit: false,
     external_urls:
      { spotify: 'https://open.spotify.com/track/6JhUq57XpJeNMxboI4Ne3S' },
     href: 'https://api.spotify.com/v1/tracks/6JhUq57XpJeNMxboI4Ne3S',
     id: '6JhUq57XpJeNMxboI4Ne3S',
     is_local: false,
     name: 'Gold Guns Girls',
     preview_url: null,
     track_number: 5,
     type: 'track',
     uri: 'spotify:track:6JhUq57XpJeNMxboI4Ne3S'},
   { disc_number: 1,
     duration_ms: 234586,
     explicit: false,
     external_urls:
      { spotify: 'https://open.spotify.com/track/4z2xy1UYMu70xCkWudeL96' },
     href: 'https://api.spotify.com/v1/tracks/4z2xy1UYMu70xCkWudeL96',
     id: '4z2xy1UYMu70xCkWudeL96',
     is_local: false,
     name: 'Gimme Sympathy',
     preview_url: null,
     track_number: 6,
     type: 'track',
     uri: 'spotify:track:4z2xy1UYMu70xCkWudeL96'}, 
   { disc_number: 1,
     duration_ms: 286320,
     explicit: false,
     external_urls:
      { spotify: 'https://open.spotify.com/track/5dmGvCOyJi943jZgiMdmDC' },
     href: 'https://api.spotify.com/v1/tracks/5dmGvCOyJi943jZgiMdmDC',
     id: '5dmGvCOyJi943jZgiMdmDC',
     is_local: false,
     name: 'Collect Call',
     preview_url: null,
     track_number: 7,
     type: 'track',
     uri: 'spotify:track:5dmGvCOyJi943jZgiMdmDC'},
   { disc_number: 1,
     duration_ms: 214440,
     explicit: false,
     external_urls:
      { spotify: 'https://open.spotify.com/track/3Ecg2M5kzSEzz6tR3fOg6g' },
     href: 'https://api.spotify.com/v1/tracks/3Ecg2M5kzSEzz6tR3fOg6g',
     id: '3Ecg2M5kzSEzz6tR3fOg6g',
     is_local: false,
     name: 'Front Row',
     preview_url: null,
     track_number: 8,
     type: 'track',
     uri: 'spotify:track:3Ecg2M5kzSEzz6tR3fOg6g'},
   { disc_number: 1,
     duration_ms: 266840,
     explicit: false,
     external_urls:
      { spotify: 'https://open.spotify.com/track/7Apb2lh3o9cYUUHziHNuNN' },
     href: 'https://api.spotify.com/v1/tracks/7Apb2lh3o9cYUUHziHNuNN',
     id: '7Apb2lh3o9cYUUHziHNuNN',
     is_local: false,
     name: 'Blindness',
     preview_url: null,
     track_number: 9,
     type: 'track',
     uri: 'spotify:track:7Apb2lh3o9cYUUHziHNuNN'},
   { disc_number: 1,
     duration_ms: 253693,
     explicit: false,
     external_urls:
      { spotify: 'https://open.spotify.com/track/73PtlKAy67oP9EGeK1fZPi' },
     href: 'https://api.spotify.com/v1/tracks/73PtlKAy67oP9EGeK1fZPi',
     id: '73PtlKAy67oP9EGeK1fZPi',
     is_local: false,
     name: 'Stadium Love',
     preview_url: null,
     track_number: 10,
     type: 'track',
     uri: 'spotify:track:73PtlKAy67oP9EGeK1fZPi'} ];


  var track = _.where(data, {disc_number: 1, track_number: 10});
console.log(JSON.stringify(track))
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<div id="content"></div>

数据对象中存在语法错误。有一个缺失}。另外,fwiw,它不是有效的JSON,因为JSON应该是字符串,但它仍然可以使用下划线。