-1

I am not familiar with javascript code, but for some reasons I am tweaking some js file available at github.

One of the lines of the source code is

JSON.stringify(a[2])

And some examples of the outputs I am getting (for the previous code) are

{"x":1,"y":1}

{"x":1,"y":1,"z":2}

I would like to modify that line of the source code to obtain outputs like

{(x,1),(y,1)}

{(x,1),(y,1),(z,2)}

instead of the ones I am getting.

Any suggestion about which code I could use to obtain such outputs?

Community
  • 1
  • 1
  • What you want as output doesn't seem to be valid Javascript code, do you just want strings? In any case, you will have to write some code to achieve that format, and Stackoverflow is not a code-writing service, you should try something first your self, including performing the necessary research, probably about Javascript objects. – Dexygen Feb 26 '20 at 23:40
  • It's possible to manually compose the desired output string using JavaScript Object and Array methods. However, why do you need this weird format? This sounds like an [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me. (and no, JSON.stringify creates [JSON](https://en.wikipedia.org/wiki/JSON#Example), and JSON only) –  Feb 26 '20 at 23:41
  • @ Dexygen: You are right about that I am only interested on the string, but you are wrong about me asking for a code-writing service. I am just asking for a suggestion, for example, it is welcome if someone could tell me which are the commands to manipulate (mainly replacing) strings inside a js file (right now I do not know what to look for). In bash I would know how to do it with a simple command line usind the "sed" command, but inside the js file I am completely stuck about where to look for. Any suggestion? – haniyi5292 Feb 26 '20 at 23:50

2 Answers2

0

First of all you must read JavaScript basics and you are talking about objects, In JavaScript, almost "everything" is an object.

  1. Booleans can be objects (if defined with the new keyword)
  2. Numbers can be objects (if defined with the new keyword)
  3. Strings can be objects (if defined with the new keyword)
  4. Dates are always objects
  5. Maths are always objects
  6. Regular expressions are always objects
  7. Arrays are always objects
  8. Functions are always objects
  9. Objects are always objects
  10. All JavaScript values, except primitives, are objects.

Objects are variables too. But objects can contain many values.

The values are written as name : value pairs (name and value separated by a colon).

Example

var person = {x:10, y:20, z:50};

just print like

var propName = JSON.stringify(a[2]);

for( propName in nyc) {
    propValue = nyc[propName]

    console.log('('+propName+','+propValue+')');
}
ANIK ISLAM SHOJIB
  • 3,002
  • 1
  • 27
  • 36
0

{(x,1),(y,1)} is not valid but [["x",1],["y",1]] is, which you can get as follows:

var arr = {"x":1,"y":1}
var res = {};
for(data in arr){
  res[data]=[data,arr[data]];
}
console.log(Object.values(res));
Sunil Lama
  • 4,531
  • 1
  • 18
  • 46