简体   繁体   English

如何在 JavaScript 中将二维数组转换为二维 object?

[英]how to convert 2d array to 2d object in JavaScript?

So I'm trying to pass some JSON objects from NODE to client side JavaScript though EJS templates.所以我试图通过 EJS 模板将一些 JSON 对象从节点传递到客户端 JavaScript 。 The issue I'm facing is that i cannot create a 2D object from my 2D array.我面临的问题是我无法从我的二维数组创建二维 object。 The code below is used in NODE to pass the object into the EJS template:下面的代码在 NODE 中用于将 object 传递给 EJS 模板:

 let json_jobs = {}; let singleJSON = {}; let array_jobs = [ ["aaaa", "title_1", "fulltime", "location1"], ["bbbb", "title_2", "parttime", "location2"], ["cccc", "title_3", "fulltime", "location3"], ["dddd", "title_4", "flex", "location4"] ]; for (let i = 0; i < array_jobs.length; i++) { singleJSON = { index: i, id: array_jobs[i][0], title: array_jobs[i][1], contract_type: array_jobs[i][2], location: array_jobs[i][3] } console.log(typeof(singleJSON )); console.log(singleJSON ); json_jobs += { job: singleJSON[i] } } console.log(typeof(json_jobs)); console.log(json_jobs); //return res.render('pages/searchJobs', {job_data: array_jobs});

The issue is that i cannot add push object using the += operator and there is no function push() to use for javascript objects?问题是我无法使用+=运算符添加 push object 并且没有 function push()可用于 javascript 对象? Any ideas how to solve this?任何想法如何解决这个问题?

This is what i want my final object to be:这就是我希望我的最终 object 成为:

    let json_jobs= {
      {
          job: {
              index: 0,
              id: "aaaa",
              title: "title1",
              contract_type: "fulltime",
              location: "location1"
          },
          job: {
              index: 1,
              id: "bbbb",
              title: "title2",
              contract_type: "parttime",
              location: "location2"
          },
          ....
      }

You could destructure the row and return an object with short hand properties .您可以解构该行并返回一个带有简写属性的 object 。

 let array_jobs = [ ["aaaa", "title_1", "fulltime", "location1"], ["bbbb", "title_2", "parttime", "location2"], ["cccc", "title_3", "fulltime", "location3"], ["dddd", "title_4", "flex", "location4"]], result = array_jobs.map(([id, country, contract_type, location], index) => ({ index, id, country, contract_type, location })); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

The job property must be an array to store multiple objects. job属性必须是一个数组来存储多个对象。

 let json_jobs = {jobs: []}; let singleJSON = {}; let array_jobs = [ ["aaaa", "title_1", "fulltime", "location1"], ["bbbb", "title_2", "parttime", "location2"], ["cccc", "title_3", "fulltime", "location3"], ["dddd", "title_4", "flex", "location4"] ]; for (let i = 0; i < array_jobs.length; i++) { singleJSON = { index: i, id: array_jobs[i][0], title: array_jobs[i][1], contract_type: array_jobs[i][2], location: array_jobs[i][3] } json_jobs.jobs.push(singleJSON); } console.log(json_jobs);

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

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