1

As I have 4 key/value in map , I'm trying to store two keys in string format and rest two in an array but it's not taking array's multiple value.

right now what output I'm getting :

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

Expected output:

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

below are the code which is returning either multiple headers value but all keys are in array format or url/status in string & headers/body in array.

 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

  }

if I add below code , it's giving me multiple value but not url/body not in string

        if (key in map) { 

      map[key].push(value) 

  } else { 

      map[key]= [value] 
  }     

In short , I'm not able to achieve both at the same time. like multiple header value with url/status in string format. Any help and suggestion would be appreciate it

Here is the full code

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}

And input are in below format:

//Status//

200


//HEADERS//

content-type = application/json


//BODY//

{ name: Fatma Zaman }


//URL//

account/43


//HEADERS//

content-type = application/text
2
  • I don't see a Map in your code. What you have a plain JavaScript object. Commented Jun 5, 2018 at 16:27
  • I haven't post the full code , now updated the code above. hopefully this will give more insight what actually i'm trying here Commented Jun 5, 2018 at 18:02

2 Answers 2

3

Please try the below code:

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 
}

This should work now, I have tested with the input. Kindly check and confirm :)

Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting below error with above solution: ERROR map[key].push(value) ^ TypeError: map[key].push is not a function
Try now I have declared map variable
Oh I already have map variable declared globally , even if tried declaring variable inside function unfortunately the error is still same :-(
Please check now, I have made some change to it.
1

I don't sure what you want, but i think this code will help you:

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;
}

I hope it can help you.

2 Comments

your solution does make sense but unfortunately It's not taking multiple value of headers :-(
You edit your input and output, i will try to fix it, my english is bad so i don't sure i understand you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.