在节点JS中自定义地图输出

时间:2018-06-05 13:09:10

标签: javascript node.js

由于我在地图中有4个键/值,我试图以字符串格式存储两个键,并且在数组中保留两个键,但它没有采用数组的多个值。

现在我得到了什么输出:

{ url: 'account/43',    
  status: '200',    
  headers: [ '\'content-type\' = \'application/json\'' ],    
  body: [ '{ "name": "Fatma Zaman" }' ]}

预期产出:

{ url: 'account/43',
  status: '200' ,
  headers: 
     [ 'content-type = application/json',  
       'content-type = application/text' ], 
  body: [ '{ name: Fatma Zaman }' ] }

下面是返回多个标题值的代码,但所有键都是数组格式或字符串&中的url / status。数组中的标题/正文。

 function processFile(content) {

    content.forEach(function(node) {

        if (node.startsWith("//")) {

            key = node.substring(2, node.length-2).toLowerCase().trim()

            return

        } else {

            value = node

        }       

        if (key in map) {

            map[key].push(value)

        } else {

            map[key]= [value]

        }

        map[key] = ["headers", "body"].includes(key)? [value] : value

     })

    return map

  }

如果我添加下面的代码,它会给我多个值但不是url / body不在字符串中

        if (key in map) { 

      map[key].push(value) 

  } else { 

      map[key]= [value] 
  }     

简而言之,我无法同时实现这两个目标。像多个标题值一样,字符串格式为url / status。任何帮助和建议都会很感激

这是完整的代码

const fs = require("fs")

const path = require("path")

let key

let value

let filePath

function parseFile(filePath) {

    filePath = path.join(__dirname, "../resources/FileData1.txt")

    fs.readFile(filePath, function(err, data) {

        if (err) throw err

        content = data.toString().split(/(?:\r\n|\r|\n)/g).map(function(line) {

            return line.trim()

        }).filter(Boolean)

        console.log(processFile(content))

    })

}

function processFile(content) {

    content.forEach(function(node) {

        if (node.startsWith("//")) {

            key = node.substring(2, node.length-2).toLowerCase().trim()

            return

        } else {

            value = node

        }

        if  (key in map) {

            map[key].push(value)

        } else {

            map[key] = value

        }

        // map[key] = ["headers", "body"].includes(key)? [value] : value



    })

    return map

}

parseFile(filePath)

module.exports = {parseFile, processFile}

输入格式如下:

//Status//

200


//HEADERS//

content-type = application/json


//BODY//

{ name: Fatma Zaman }


//URL//

account/43


//HEADERS//

content-type = application/text

2 个答案:

答案 0 :(得分:3)

请尝试以下代码:

function processFile(content) {
let map ={};
content.forEach(function(node) {

    if (node.startsWith("//")) {

        key = node.substring(2, node.length-2).toLowerCase().trim()

        return

    } else {

        value = node

    }       

    if (["headers", "body"].includes(key)) {
        if(map[key]){
            map[key].push(value);
        }else{
            map[key] = [value]
        }
    } else {
        map[key]= value
    }
 })

return map 
}

现在应该可以使用,我已经测试了输入。请检查并确认:)

答案 1 :(得分:1)

我不确定你想要什么,但我认为这段代码可以帮到你:



var yourObj = {
    url: 'account/43',
    status: '200',
    headers: ['\'content-type\' = \'application/json\''],
    body: ['{ "name": "Fatma Zaman" }']
};

console.log('Your obj:');
console.log(yourObj);

console.log('formatted:');
console.log(format(yourObj));


function format(obj) {
    for (var prop in obj) {
        if (typeof obj[prop] != 'object') {
            obj[prop] = obj[prop].replace(/"|'/g, '');
        } else {
            obj[prop] = format(obj[prop]);
        }
    }
    return obj;
}




我希望它可以帮到你。

相关问题