最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create an Array of Favorite fruits Object - Stack Overflow

programmeradmin1浏览0评论

I have the following assessment which is to Create Array of Favorite Food items object in data.json file.

The Array of Objects should have the following fields :

  • Name
  • Type
  • Price

After writing the JSON data, this file should be imported in loopobject.js.

I tried the above request with the below data.json

{
 "food":[
    {
      "Name":"Apple",
      "Type":"fruit",
      "Price":100
    },
    {
      "Name":"pizza",
      "Type":"italian",
      "Price":370
    },
    {
      "Name":"Burger",
      "Type":"Mac&Cheese",
      "Price":310
    },
    {
      "Name":"salad",
      "Type":"Veg",
      "Price":50
    }
  ]
}

And with loopobject.js

var json = require('./data.json');

json.forEach(function(object) { console.log(object.Name); });

and the Schema is:

const schema = Joi.array().min(3).has({
    Name: Joi.string().required(),
    Type: Joi.string().required(),
    Price: Joi.number().required(),
});

On submitting the above data.json and loopobject.js I get:

ValidationError: "value" must be an array

I am not sure how to edit the data.json to make it Array of Objects, as for as I can think the data.json has array of objects. Can someone help fix the issue on data.json and loopobject.js?

Please note I can't edit the JSON schema

I have the following assessment which is to Create Array of Favorite Food items object in data.json file.

The Array of Objects should have the following fields :

  • Name
  • Type
  • Price

After writing the JSON data, this file should be imported in loopobject.js.

I tried the above request with the below data.json

{
 "food":[
    {
      "Name":"Apple",
      "Type":"fruit",
      "Price":100
    },
    {
      "Name":"pizza",
      "Type":"italian",
      "Price":370
    },
    {
      "Name":"Burger",
      "Type":"Mac&Cheese",
      "Price":310
    },
    {
      "Name":"salad",
      "Type":"Veg",
      "Price":50
    }
  ]
}

And with loopobject.js

var json = require('./data.json');

json.forEach(function(object) { console.log(object.Name); });

and the Schema is:

const schema = Joi.array().min(3).has({
    Name: Joi.string().required(),
    Type: Joi.string().required(),
    Price: Joi.number().required(),
});

On submitting the above data.json and loopobject.js I get:

ValidationError: "value" must be an array

I am not sure how to edit the data.json to make it Array of Objects, as for as I can think the data.json has array of objects. Can someone help fix the issue on data.json and loopobject.js?

Please note I can't edit the JSON schema

Share Improve this question edited Jan 24, 2020 at 14:53 Qhori 15712 bronze badges asked Jan 24, 2020 at 14:12 Vikram MohanVikram Mohan 393 silver badges16 bronze badges 7
  • 1 data.json doesn't contain an array of objects, it contains an object with a key named "food" and an array of objects as it's value. Also what are those quotes? There should only be " in a JSON file. – Luca Kiebel Commented Jan 24, 2020 at 14:16
  • if this is the actual data your data.json file has, I can see many ”s where there should be " s – Risalat Zaman Commented Jan 24, 2020 at 14:16
  • There is no such thing as a JSON Object. – Heretic Monkey Commented Jan 24, 2020 at 14:17
  • If a question has these tags: javascript, arrays and object, there is a very high probability that it's been already answered here on SO. If you can't find a similar question, then look again at your code and find an obvious typo. In this case it seems to be the latter ;) Fix all the typos and see if that solves your problem. – Sebastian Kaczmarek Commented Jan 24, 2020 at 14:21
  • The " quote change is because of the notepad. I copied code from notepad to the site. Please ignore that. – Vikram Mohan Commented Jan 24, 2020 at 14:38
 |  Show 2 more ments

3 Answers 3

Reset to default 3

json is not an array, you want to use json.food.

var json = require('./data.json');

json.food.forEach(function(object) { console.log(object.Name); });

Your json is an object and has "food" named array. If you want to have just array, please change your JSON like this:

[
    {
      "Name":"Apple",
      "Type”:”fruit”,
      "Price":100
    },
    {
      "Name”:”pizza”,
      "Type”:”italian”,
      "Price”:370
    },
    {
      "Name”:”Burger,
      "Type”:”Mac&Cheese”,
      "Price”:310
    },
    {
      "Name”:”salad”,
      "Type”:”Veg”,
      "Price”:50
    }
  ]

sure its not an array its an object with key and array pair {food:[array]} first you need to extract that array part and then you can perform the array operations

发布评论

评论列表(0)

  1. 暂无评论