score:5
you can use object.keys
and map
:
var obj = {
word1 : 1,
word2 : 1,
word3 : 2,
word4 : 3
}
var array = object.keys(obj).map((key) => ({
word: key,
count: obj[key]
}));
console.log(array);
score:0
you can use reduce()
on the object.keys
for this:
let o = {
word1: 1,
word2: 1,
word3: 2,
word4: 3
};
let arr = object.keys(o).reduce((a, b) => a.concat({
word: b,
count: o[b]
}), []);
console.log(arr);
score:0
this should work.
const start = {
word1 : 1,
word2 : 1,
word3 : 2,
word4 : 3
};
const finish = object.keys(start).map(k => { word: k, count: start[k] });
score:0
you should push an object for each key in the object:
var result = [];
for (var key in p) {
if (p.hasownproperty(key)) {
result.push({
word: key,
count: p[key]
});
}
}
score:0
you can use object.keys()
to iterate through each key in the object.
use map()
or foreach()
to get the desired result.
let o = {
word1: 1,
word2: 1,
word3: 2,
word4: 3
};
var result = [];
object.keys(o).foreach(function(x){
result.push({word: x, count : o[x]});
});
console.log("result with foreach :" ,result);
var resultwithmap = object.keys(o).map( x =>
({ word : x, count : o[x] }) );
console.log("result with map :" , resultwithmap);
Source: stackoverflow.com
Related Query
- Transform a single object into an array of objects
- Merging arrays that hold CSV data into one object array
- Javascript manipulate data into array of object for d3 stacked chart
- D3 sort an array of objects on 2 object properties
- Single stacked bar with array of objects
- Transform parsed percentage data into new array
- 'push' new number into an array of objects
- Filter nested array of objects by object property in d3.js
- How to manipulate JSON objects into an array with javascript
- how to transform a hierarchical csv (semicolon separated) into a multi-dimensional array
- How to transform JS nested object keys to hierarchical array schema
- Replace object in array with two objects
- Javascript returns undefined when indexing into an array using a property of an object
- Default values in json object based on contents of other objects in the same array
- Javascript: Array of Objects is returning undefined when accessing to a single Element. Even though there are 696 Objects saved in it
- Splitting an object key name (separated by a comma) generating a new array of objects
- D3 taking an array of two objects and reading it into a D3 shape
- adding objects into an existing object
- Converting an array to an object of nested objects for a tree diagram in Javascript
- Making a bar chart in d3.js. How to put values in a single object into one bar?
- Convert sub-array of objects with key-value pairs into array of arrays
- d3.js pushing object into another object which has array of object
- Dimplejs can deal with array of objects in each Json Object
- Is there a way to tell crossfilter to treat elements of array as separate records instead of treating whole array as single key?
- How to get maximum value from an array of objects to use in d3.scale.linear().domain()
- How to convert a jQuery object into a d3 object?
- What object type is the instance element parameter pass into the link function of an angular directive?
- How do you access an array of Objects using D3?
- d3: A sub array of objects
- finding d3 extent of multiple values in object array
More Query from same tag
- Hightlight the respective layer in stacked Bar chart when mouseover on legend in d3
- dcjs y axis on ordinal bar charts?
- Event Handling in nvd3
- D3 charts : How to open a radar ticket by clicking on the graph.?
- scaleBand() shifts the values left
- New to d3, unable to show graph with json
- ReferenceError: event is not defined firefox d3.js
- IntelliJ IDEA Ultimate: Code completion for JS included via simple script tag
- D3.js click being blocked by X-XSS-Protection using chrome
- python-nvd3 missing xAxis ticks
- show div and hide other ones - d3.js
- JavaScript, a good way to represent a float as color in a plot
- Making y axis dashed in c3js
- Is there a drop event for d3.js?
- Setting up Scale Domain and Bar Width for multiple bar charts in one page
- Load and parse multiple csv files with D3.js
- What is causing certain data to display weird in this d3 chart?
- Align text node centrally(horizontally) inside group element
- Merging json and csv using topojson
- Angularjs capture custom directive data back to controller
- d3.js dynamic Tooltip for Chord diagram
- How to save the state of the Map view using angularjs and in JSON format?
- How to insert enter() selections at locations corresponding to its data?
- How can I copy the content of my SVG and append it to another SVG frame?
- D3Js v3: Path not showing up on Node Elements
- d3.js events registered but not triggering
- d3.js Attributes, Transform, Translate when using polylines
- Dragging a globe in D3
- While creating a bar graph using d3 how to associate the bar height with the numbers on the scale?
- D3.select(this) but filter