简体   繁体   English

尝试“映射”嵌套的 JSON 元素对象(javascript)

[英]Trying to 'map' nested JSON element object (javascript)

I am trying to 'map' nested JSON elements that have objects in order to build HTML.我正在尝试“映射”具有对象的嵌套 JSON 元素以构建 HTML。 I am not sure what I am doing wrong with the syntax as follows:我不确定我在语法上做错了什么,如下所示:

    array1 = [
      {
        "name":"test",
        "things": [
          { "name":"thing1" },
          { "name": "thing2"}
        ]
      }
    ];

    const createThingy = (item) => `
        <p>${item.name}</p>
    `

    // pass a function to map
    const map1 = array1.things.map(createThingy).join('');
    console.log(array1);

    // expected output: <p>thing1</p><p>thing2</p>

Thank you in advance for your time and consideration.提前感谢您的时间和考虑。

Think of the array as an object.将数组视为一个对象。 It's accessed in a similar way, so if it were an object it would be like this:它以类似的方式访问,所以如果它是一个对象,它会是这样的:

let array1 = {
  0: {
    "name":"test",
    "things": [
      { "name": "thing1" },
      { "name": "thing2" }
    ]
  }
};

Therefore, to access its first element directly you need:因此,要直接访问其第一个元素,您需要:

array1[0].things

To get your desired outcome you need to the following:要获得您想要的结果,您需要执行以下操作:

 let array1 = [ { "name": "test", "things": [ { "name": "thing1" }, { "name": "thing2" } ] } ]; const createThingy = (item) => ` <p>${item.name}</p> `; // pass a function to map const map1 = array1[0].things.map(createThingy).join(''); console.log(map1);

In case your array can have multiple elements, you can use the following:如果您的数组可以有多个元素,您可以使用以下内容:

 let array1 = [ { "name": "test", "things": [ { "name": "thing1" }, { "name": "thing2" } ] } ]; const createThingy = (item) => ` <p>${item.name}</p> `; // pass a function to map const map1 = array1.reduce((acc, elem) => acc + elem.things.map(createThingy).join(''), ""); console.log(map1);

 array1 = [ { "name":"test", "things": [ { "name":"thing1" }, { "name": "thing2"} ] } ]; const createThingy = (item) => ` <p>${item.name}</p> ` // pass a function to map const map1 = array1[0].things.map(createThingy).join(''); console.log(array1); console.log(map1);

As Nick Parsons said, you have to loop over the array1 array to get things property.正如 Nick Parsons 所说,您必须遍历array1数组才能获取things属性。

const array1 = [
  {
    "name":"test",
    "things": [
      { "name":"thing1" },
      { "name": "thing2"}
    ]
  }
];

const createThingy = (item) => `
    <p>${item.name}</p>
`

// pass a function to map
const map1 = array1[0].things.map(createThingy).join('');

console.log(array1);
console.log(map1);

Also, be advised that if your array1 variable is empty or in case there is no things attribute in preferred index, your code code will give error.另外,请注意,如果您的array1变量为空,或者如果首选索引中没有things属性,您的代码代码将出错。 Be sure to check if they are empty.请务必检查它们是否为空。 You can do this by using lodash isEmpty function.你可以通过使用 lodash isEmpty函数来做到这一点。

You have to loop over the array1 to get the desired output as Nick Parsons said in the comments.正如 Nick Parsons 在评论中所说,您必须遍历 array1 以获得所需的输出。

array1 = [
      {
        "name":"test",
        "things": [
          { "name":"thing1" },
          { "name": "thing2"}
        ]
      }
    ];

    const createThingy = (item) => `
        <p>${item.name}</p>
    `

array1.map(item => {
item.map(key => createThingy(key).join(''));
});

    // expected output: <p>thing1</p><p>thing2</p>

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

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