使用JSON文件作为Node.JS中的数组

huangapple go评论89阅读模式
英文:

Using JSON File as an Array in Node.JS

问题

I've been having trouble converting a .json file into an array object in NodeJS,

This is my JSON:

{
    "cat": {
      "nani": "meow"
    },
    "dog": {
      "nani": "woof"
    }
}

index.js:

const array = require('../../data/usershops.json');
var shop = array[0].nani
return shop;

The output in console is:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'nani' of undefined

It actually returns a value if I used this:

array["cat"].nani // => "meow"

How can I get the index key?

英文:

I've been having trouble converting a .json file into an array object in NodeJS,

This is my JSON:

{
    "cat": {
      "nani": "meow"
    },
    "dog": {
      "nani": "woof"
    }
}

index.js:


const array = require('../../data/usershops.json');
var shop = array[0].nani
return shop;

The output in console is:

>UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'nani' of undefined

It actually returns a value if I used this:

array["cat"].nani // => "meow"

How can I get the index key?

答案1

得分: 1

require() 会为您进行JSON解析并返回一个对象。

您可以使用Object.values()来获取一个仅包含此对象值的数组(忽略键):

const data = require('../../data/usershops.json');
const arr = Object.values(data)

console.log(arr);
// [ { nani: 'meow' }, { nani: 'woof' } ]

但请注意,对象中键/值的顺序是不确定的,这意味着Object.values()返回的数组中值的顺序可能不总是您期望的顺序。<br>
除非您在数组上进行迭代并使用不依赖于它们顺序的操作,否则建议不要以这种方式使用对象。

英文:

require() does the JSON parsing for you and returns an object.

You can use Object.values() to get an array that contains only the values of this object (and ignore the keys):

const data = require(&#39;../../data/usershops.json&#39;);
const arr = Object.values(data)

console.log(arr);
// [ { nani: &#39;meow&#39; }, { nani: &#39;woof&#39; } ]

But please be aware that the order of keys/values in an object is not determined and this means the order of values in the array returned by Object.values() might not always be the one you expect.<br>
Unless you iterate over the array and use all the values in on operation that does not depend on their order, I recommend you don't use an object this way.

答案2

得分: 0

let data = {
    "cat": {
      "nani": "meow"
    },
    "dog": {
      "nani": "woof"
    }
};

let array = Object.entries(data).map(([key,value])=>value);

console.log(array[0].nani);
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let data = {
    &quot;cat&quot;: {
      &quot;nani&quot;: &quot;meow&quot;
    },
    &quot;dog&quot;: {
      &quot;nani&quot;: &quot;woof&quot;
    }
};

let array = Object.entries(data).map(([key,value])=&gt;value);

console.log(array[0].nani);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 20:40:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定