Converting JavaScript object with numeric keys into array
It's actually very straight forward with jQuery's $.map
var arr = $.map(obj, function(el) { return el });
FIDDLE
and almost as easy without jQuery as well, converting the keys to an array and then mapping back the values with Array.map
var arr = Object.keys(obj).map(function(k) { return obj[k] });
FIDDLE
That's assuming it's already parsed as a javascript object, and isn't actually JSON, which is a string format, in that case a run through JSON.parse
would be necessary as well.
In ES2015 there's Object.values
to the rescue, which makes this a breeze
var arr = Object.values(obj);
How to convert an Object {} to an Array [] of key-value pairs in JavaScript
You can use Object.keys()
and map()
to do this
var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}
var result = Object.keys(obj).map((key) => [Number(key), obj[key]]);
console.log(result);
Prevent node.js from converting a nested object with numeric keys into array
Try adding the contentType
header and using JSON.stringify
to serialize the object before passing to $.ajax
:
const express = require("express");
const app = express();
const html = `<!DOCTYPE html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js">
</script>
</head>
<body>
<script>
$.ajax({
url: "/cctool/report",
method: "PUT",
contentType: "application/json",
data: JSON.stringify({
new: {
10: "Test",
20: "Hello",
}
}),
success: data => console.log(data),
});
</script></body></html>`;
app
.use(express.json())
.get("/", (req, res) => res.send(html))
.put("/cctool/report", (req, res) => {
console.log(req.body);
res.json(req.body);
})
.listen(3000);
See jQuery posting JSON.
Is there a way to pass keys into a JavaScript array where a a string might match multiple values of an object?
Use filter
instead of find
and map
to remove the values.
const obj = { quay: "foo", key1: "blargh", yaya: "foo", idnet: "blat", blah: "foo", hahas: "blargh" }
const str = 'foo';
const keys = Object.entries(obj).filter(([, val]) => val === str).map(([key]) => key);
console.log(keys);
Related Topics
How to Add Number of Days to Today'S Date
How to Match a Whole Word in JavaScript
Indexof Method in an Object Array
What's the Right Way to Decode a String That Has Special HTML Entities in It
Jquery Drag/Resize with CSS Transform Scale
How to Make Changeable Themes Using CSS and JavaScript
CSS Transition Doesn't Work with Top, Bottom, Left, Right
How to Build & Deploy a Samsung Smarttv App Without the Ide (E.G: on Linux)
What's the Difference Between Event.Stoppropagation and Event.Preventdefault
How to Deep Clone in JavaScript
JavaScript Object Literal: What Exactly Is {A, B, C}
How to Replace All Line Breaks in a String With ≪Br /≫ Elements
What Is the JavaScript ≫≫≫ Operator and How to Use It
Use :Hover to Modify the CSS of Another Class
Webkit and Jquery Draggable Jumping
Detect CSS Transitions Using JavaScript (And Without Modernizr)