javascript - 单个数组中的 Json 对象,需要使用 key meta_value 拆分键上的值

标签 javascript arrays json split javascript-objects

我在单个数组中有一个 json 对象,这是一个原始文件,我删除了一些字段,现在我想改变每个条目的键值之一。这是一些 Json 示例。我想循环遍历并分割 http://www.website/wp-content/uploads/ 处的 meta_value 现在我的代码将每个meta_value返回为未定义,而不是拆分值,我认为这与循环更改键值并尝试在之后拆分它有关。

任何帮助将不胜感激

这是我到目前为止在最后生成 json 数据的代码

var exclusions = [
"ID", "post_author", "post_date_gmt", "post_excerpt", "comment_status", 
"ping_status", "post_password", "to_ping" ,"pinged", "post_modified","post_name",
"post_modified_gmt", "post_content_filtered", "guid", "menu_order", "post_mime_type",
"comment_count", "meta_id", "post_id", "post_type", "post_status"
];
var a = JSON.parse(fs.readFileSync('final.json'));

a.forEach(obj=>{
  exclusions.forEach(excl=>{
    if(obj[excl] || obj[excl] === ""){
      delete obj[excl];
    }

    if(obj["meta_value"] !== undefined){

      let objTest = obj["meta_value"].split('http://www.fsd.ca/wp-content/uploads/')[1];

      obj["meta_value"] = objTest;


    }

  });
});

console.log(a);

初始排除后的 Json

[
  {
    post_date: '2012-02-16 23:37:22',
    post_content: `Today we worked at literacy centres. We are writing our own Three Bears story. We are reading with Mrs.Kitson, writing in our life books, working in our printing books and working on making words on the iPads. We use the apps Pocket phonics, magnet board and Montessori crosswords. We went to the library. In the afternoon we went to the gym and watched a play by Quest Theatre. It was called <span style="text-decoration: underline;">For</span> <span style="text-decoration: underline;">Art's Sake.</span> They told us that we are all artists and that we should use our imagination. We did zumba. We danced to The chihuahua song. We went to the DPA room and played different tag games. Our hearts worked hard. We had a fun day!`,
    post_title: 'Hometime',
    meta_value: 'http://www.fsd
  1. 列出项目

.ca/wp-content/uploads/2012/02/SN850631.jpg' }, { post_date: '2012-02-21 20:39:19', post_content:'今天我们开始 build 我们的城堡。我们给它们涂上彩色的薄纸。我们正在书写我们自己的童话。我们读了《 Frog 公主童话》。我们正在制作泥龙和缝制木偶。我们去了健身房,然后就逃课了。我们在生活中写书并练习打印。我们度过了愉快的一天!', post_title: '创建城堡', 元值:'http://www.fd.ca/wp-content/uploads/2012/02/SN850649.jpg ' }, { post_date: '2012-02-23 21:30:55', post_content: '我们讨论了字母 E。E 是元音。每个单词都有一个元音。我们做了E表。我们去滑冰了。滑冰后我们喝了热巧克力。我们在新的扫盲中心工作。我们正在努力工作。我们去了音乐。我们玩了大黄蜂游戏。我们去了 DPA 房间玩标签和跳舞。我们去了中心和图书馆。我们度过了愉快的一天!', post_title: '我们有新枕头', 元值:'http://www.fds.ca/wp-content/uploads/2012/02/SN850660.jpg ' }, { post_date: '2012-02-24 19:06:39', post_content:'今天是夏威夷日。我们穿着短裤和夏天的衣服。我们制作了一张 Chicka Chicka Boom Boom 图片。我们做了花环。我们吃了面糊菠萝,还吃了芒果、菠萝、木瓜、杨桃、火龙果和椰子。我们尝试了椰子水。我们跳了 hell 边境并跳了夏威夷风格的舞蹈。我们坐在毛巾上玩数学游戏,猜杯子下面有多少糖果。我们写下了人生的书,然后去了大公园。我们度过了愉快、有趣的一天!', post_title: '夏威夷日', 元值:'http://www.ff.ca/wp-content/uploads/2012/02/SN850665.jpg ' }, ... 654 更多项目 ]

meta_value键值改变后的Json,每个值都是未定义的

    [
{
    post_date: '2012-02-16 23:37:22',
    post_content: `Today we worked at literacy centres. We are writing our own Three Bears story. We are reading with Mrs.Kitson, writing in our life books, working in our printing books and working on making words on the iPads. We use the apps Pocket phonics, magnet board and Montessori crosswords. We went to the library. In the afternoon we went to the gym and watched a play by Quest Theatre. It was called <span style="text-decoration: underline;">For</span> <span style="text-decoration: underline;">Art's Sake.</span> They told us that we are all artists and that we should use our imagination. We did zumba. We danced to The chihuahua song. We went to the DPA room and played different tag games. Our hearts worked hard. We had a fun day!`,
    post_title: 'Hometime',
    meta_value: undefined
  },
  {
    post_date: '2012-02-21 20:39:19',
    post_content: 'Today we started making our castles. We painted them colourful tissue paper. We are writing our own fairy tales. We read The Princess Frog Fairy Tale. We are making clay dragons and sewing puppets. We went to the gym and skipped. We wrote in our life books an practiced our printing. We had a fun day!',
    post_title: 'Creating Castles',
    meta_value: undefined
  },
  {
    post_date: '2012-02-23 21:30:55',
    post_content: 'We talked about the letter E. E is a vowel. There is a vowel in every word. We did our E sheets. We went skating. We had hot chocolate after skating. We worked at new literacy centers. We are doing hard work. We went to Music. We played the bumble bee game. We went to the DPA room to play tag and dance. We went to centers and to the library. We had a fun day!',
    post_title: 'We Have New Pillows',
    meta_value: undefined
  },
  {
    post_date: '2012-02-24 19:06:39',
    post_content: 'Today it was Hawaii Day. We wore shorts and summer clothes. We made a Chicka Chicka Boom Boom picture. We made leis. We ate batter dipped pineapples and ate mango, pineapple, papaya, starfruit, dragon fruit and coconut. We tried coconut water. We did the limbo and danced Hawaii style. We sat on our towels and played a math game where we guessed how many candies were under the cup. We wrote in our life books and we went to the big park. We had a fun, fun day!',
    post_title: 'Hawaii Day',
    meta_value: undefined
  },
  ... 654 more items
]

最佳答案

问题是您有代码可以在 exclusions.forEach() 循环内更改 obj["meta_value"] 。因此它对同一个对象运行多次。第一次,它将 'http://www.mrskitson.ca/wp-content/uploads/2012/02/SN850631.jpg' 替换为 '2012/02/SN850631.jpg '。第二次,它在元值中找不到分隔符 'http://www.mrskitson.ca/wp-content/',因此 split()返回一个只有 1 个元素的数组,并且 [1] 未定义。

将其从内循环中取出,它应该可以正常工作。

var exclusions = [
  "ID", "post_author", "post_date_gmt", "post_excerpt", "comment_status",
  "ping_status", "post_password", "to_ping", "pinged", "post_modified", "post_name",
  "post_modified_gmt", "post_content_filtered", "guid", "menu_order", "post_mime_type",
  "comment_count", "meta_id", "post_id", "post_type", "post_status"
];
var a = [
  {
    post_date: '2012-02-16 23:37:22',
    post_content: `Today we worked at literacy centres. We are writing our own Three Bears story. We are reading with Mrs.Kitson, writing in our life books, working in our printing books and working on making words on the iPads. We use the apps Pocket phonics, magnet board and Montessori crosswords. We went to the library. In the afternoon we went to the gym and watched a play by Quest Theatre. It was called <span style="text-decoration: underline;">For</span> <span style="text-decoration: underline;">Art's Sake.</span> They told us that we are all artists and that we should use our imagination. We did zumba. We danced to The chihuahua song. We went to the DPA room and played different tag games. Our hearts worked hard. We had a fun day!`,
    post_title: 'Hometime',
    meta_value: 'http://www.mrskitson.ca/wp-content/uploads/2012/02/SN850631.jpg'
  },
  {
    post_date: '2012-02-21 20:39:19',
    post_content: 'Today we started making our castles. We painted them colourful tissue paper. We are writing our own fairy tales. We read The Princess Frog Fairy Tale. We are making clay dragons and sewing puppets. We went to the gym and skipped. We wrote in our life books an practiced our printing. We had a fun day!',
    post_title: 'Creating Castles',
    meta_value: 'http://www.mrskitson.ca/wp-content/uploads/2012/02/SN850649.jpg'
  },
  {
    post_date: '2012-02-23 21:30:55',
    post_content: 'We talked about the letter E. E is a vowel. There is a vowel in every word. We did our E sheets. We went skating. We had hot chocolate after skating. We worked at new literacy centers. We are doing hard work. We went to Music. We played the bumble bee game. We went to the DPA room to play tag and dance. We went to centers and to the library. We had a fun day!',
    post_title: 'We Have New Pillows',
    meta_value: 'http://www.mrskitson.ca/wp-content/uploads/2012/02/SN850660.jpg'
  },
  {
    post_date: '2012-02-24 19:06:39',
    post_content: 'Today it was Hawaii Day. We wore shorts and summer clothes. We made a Chicka Chicka Boom Boom picture. We made leis. We ate batter dipped pineapples and ate mango, pineapple, papaya, starfruit, dragon fruit and coconut. We tried coconut water. We did the limbo and danced Hawaii style. We sat on our towels and played a math game where we guessed how many candies were under the cup. We wrote in our life books and we went to the big park. We had a fun, fun day!',
    post_title: 'Hawaii Day',
    meta_value: 'http://www.mrskitson.ca/wp-content/uploads/2012/02/SN850665.jpg'
  },
];

a.forEach(obj => {
  exclusions.forEach(excl => {
    if (obj[excl] || obj[excl] === "") {
      delete obj[excl];
    }
  });
  if (obj["meta_value"] !== undefined) {
    let objTest = obj["meta_value"].split('http://www.mrskitson.ca/wp-content/uploads/')[1];
    obj["meta_value"] = objTest
  }
});

console.log(a);

关于javascript - 单个数组中的 Json 对象,需要使用 key meta_value 拆分键上的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59872458/

相关文章:

javascript 函数第二次不工作

javascript - 将复杂的二维数组合并为一个

javascript - 将每个动态创建的数组中的第一项添加到一起

arrays - 单击按钮时显示数据列表

json - 货币/货币值(value)——如何存储在数据库中并使用 JSON 传输?

将类 "constructor function"转换为单例的 Javascript 实用函数

javascript - 如何将函数从 Node Horseman 传递给 `evaluate` 函数?

javascript - 如何在bot框架版本4 javascript中使用cardImage接口(interface)的tap属性

php - 显示数组 php 时出现问题

json - 有没有比我使用的更好的方法来 "Pretty Print"json 字符串