How to convert a js object into an array? - javascript

My data:
{product_quantity: {quantity: 13, code: "AAA", warehouse: "1000",}}
product_quantity is an field from a mongodb json data.
I need to convert it into this:
{"product_quantity": [{"quantity": 13, "code": "AAA", "warehouse": "1000"}]
How can I transform an object into array with its keys and values?
Thanks.

json you want is not valid , you can use this
let productQuantity={product_quantity:{quantity:13, code:"AAA", warehouse:"1000"}};
productQuantity.product_quantity=[productQuantity.product_quantity];
console.log(JSON.stringify(productQuantity));
result
{"product_quantity":[{"quantity":13,"code":"AAA","warehouse":"1000"}]}

const data = [quantity];
output;
[{quantity: 13, code: "AAA", warehouse: "1000",}];

Related

How to send array of objects in postman form data and and receive as array of objects

In postman, I have done in form-data KEY -products VALUE-
[
{ name: "Pizza", price: "10", quantity: "7" },
{ name: "Cerveja", price: "12", quantity: "5" },
{ name: "Hamburguer", price: "10", quantity: "2" },
{ name: "Fraldas", price: "6", quantity: "2" },
];
In code,I want to receive this same array of objects but I can receive it as a string. My code is
var products =req.body.products;
console.log(typeof(products))//string shows
var Products=JSON.parse(JSON.stringify(products))
console.log(typeof(Products))// it also string shows
if I print array 0 index value it prints "[" array bracket
Please Help me out I am new in this.
Thank you
I think you have ask duplicate question
Click here! that solution worked for me!
Since products is a string, you only need to parse this into an object with JSON.parse()
Use JSON.stringify() when u want to convert a javascript object to a string.
So:
var products = JSON.parse(req.body.products); // parse string to object
console.log(products[0]); // will output the pizza

How to manipulate jsonl objects

Given an API that returns a jsonl, how can I manipulate the data that I obtain?
What if the API gives me data like this:
{"plate": "pizza", "quantity": 3}
{"plate": "pasta", "quantity": 2}
In javascript the object retrieved what type will have?
If I want to add a new object, to have a result like:
{"plate": "pizza", "quantity": 3}
{"plate": "pasta", "quantity": 2}
{"plate": "hotdog", "quantity": 7}
How can I do that maintaining the type .jsonl and not creating and array?
Thanks a lot for the help
According to jsonlines.org, each line in a jsonl file is a valid JSON value.
Thus, the approach would seem to be:
split the file into lines.
parse each line separately as JSON.
Something like this:
const lines = data.split(/\n/);
lines.forEach(line => {
const object = JSON.parse(line);
// Do something with object.
});

JSON to object array format

I want to display like convert JSON data to array objects how can solve this problem using jquery or js?
JSON data :
[{
"new": 122,
"old": 3389,
"boarding": 1,
"aws": 10,
"azure": 12,
"cli": 41
}];
To array object:
[
["new", 122],
["old", 3389],
["boarding", 1]
]
one more pattern I need it I have an array like this in Ruby
[37,
3,
261,
1,
0,
1457]
to convert into add as key entry static
[
["new",37],
["old",3],
["boarding",1]
["aws",0]
["azure",1457]
]
Firstly note that you do not need jQuery at all for this. jQuery is primarily a tool for amending the DOM. To work with data structures like this native JS is all you need.
To achieve what you require, you can use Object.keys() to get the keys of the 0th object in your original array, then you can loop over them using map() to build a new 2 dimensional array from that data:
var originalArr = [{
"new": 122,
"old": 3389,
"boarding": 1,
"aws": 10,
"azure": 12,
"cli": 41
}];
var newArr = Object.keys(originalArr[0]).map(function(key) {
return [key, originalArr[0][key]];
});
console.log(newArr);
Its simple enough. Just enter the array into Object.entries(). like this Object.entries(originalArr[0])
A simple one-line alternative:
const data = [{
"new": 122,
"old": 3389,
"boarding": 1,
"aws": 10,
"azure": 12,
"cli": 41
}];
const result = Object.keys(data[0]).map(k => ([k, data[0][k]]));
console.log(result);

map an array from an array of objects key/value

I need to take an array objects that and map it so that the new array is just a simple array if each object's id.
So for example:
[
{id: 49, name: "Rest update test"},
{id: 12, name: "Rest test"}
]
would become:
[49, 12]
i have tried this so far:
myObject.map(object => object.id);
so my actual function that is not working is the following, when I view the console.log, it is showing the original object:
onSubmit() {
this.userForm.value.accountsToAdd.map(object => object.id);
console.log(this.userForm.value.accountsToAdd);
}
Assuming the given code does not work, then you need to assign the result of mapping.
Array#map does not mutate the original array but return a new one.
var array = [{ id: 49, name: "Rest update test" }, { id: 12, name: "Rest test" }],
ids = array.map(object => object.id);
console.log(ids);

Reading CSV in D3 and converting it to array of arrays

I have the following small dataset hardcoded into a variable in my code:
var dataset = [['CENTRAL', 44, 552, 18565],
['NORTHERN', 42, 945, 20092],
['SOUTHERN', 96, 795, 30095],
['PARK', 1, 640, 9341],
['MISSION', 66, 1198, 18542],
['TENDERLOIN', 23, 113, 10735],
['RICHMOND', 9, 561, 9082],
['TARAVAL', 81, 789, 11966],
['INGLESIDE', 5, 1368, 13414],
['BAYVIEW', 7, 985, 14711]];
Now, this dataset is taken directly from a .csv file, that looks like this:
District,Prostitution,VehicleTheft,Totalcrimecount
CENTRAL,44,552,18565
NORTHERN,42,945,20092
SOUTHERN,96,795,30095
PARK,1,640,9341
MISSION,66,1198,18542
TENDERLOIN,23,113,10735
RICHMOND,9,561,9082
TARAVAL,81,789,11966
INGLESIDE,5,1368,13414
BAYVIEW,7,985,14711
However, I'd obviously like to be able to just read in the file, which I've attempted using this:
var dataset_csv = d3.csv("datafile.csv", function(d){
console.log(dataset_csv = d)
});
Which gives me an array of objects that look like this:
[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
District: "CENTRAL"
Prostitution: "44"
Totalcrimecount: "18565"
VehicleTheft: "552"
My question is, and it might be trivial, how can I transform those objects into an array data structure like my initial dataset? I've been battling with it for some time now. Any hints greatly appreciated.
Use Array#map to iterate over each object and Object.keys to catch only the keys values.
var dataset_csv = [{District: "CENTRAL", Prostitution: "44", Totalcrimecount: "18565", VehicleTheft: "552"}, {District: "WEST", Prostitution: "22", Totalcrimecount: "4324", VehicleTheft: "53"}, {District: "EAST", Prostitution: "11", Totalcrimecount: "23434" , VehicleTheft: "76"}],
datasetArr = dataset_csv.map(v => Object.keys(v).map(c => Number(v[c]) ? Number(v[c]) : v[c]));
console.log(datasetArr);

Categories