Reputation: 15
I have this function that loops through some object properties.
It returns this:
Central Park: 0,1,1,1,2,1,3,1,4,1,5,1,6,1,7,1
Juniper Valley Park: 2,0
Highland Park: 2,0,5,0
var parks = [
{
name: 'Central Park',
landmarks: [[0,1],[1,1],[2,1],[3,1],[4,1],[5,1],[6,1],[7,1]]
},
{
name: 'Juniper Valley Park',
landmarks: [2,0]
},
{
name: 'Highland Park',
landmarks: [[2,0],[5,0]]
},]
function showLandmarks() {
for (var i = 0, len = parks.length; i < len; i++) {
console.log(parks[i].name +': '+parks[i].landmarks);
}
}
showLandmarks();
I would like to create pairs out of the returned data. The expected result is:
Central Park: [[0,1],[1,1],[2,1],[3,1],[4,1],[5,1],[6,1],[7,1]]
Juniper Valley Park: [2,0]
Highland Park: [[2,0],[5,0]]
I found a few topics here but only partial information.
Upvotes: 0
Views: 84
Reputation: 994
It's pretty difficult to manage through the Juniper entry since it isn't the same format, but the code below does the trick:
var parks = [
{
name: 'Central Park',
landmarks: [[0,1],[1,1],[2,1],[3,1],[4,1],[5,1],[6,1],[7,1]]
},
{
name: 'Juniper Valley Park',
landmarks: [2,0]
},
{
name: 'Highland Park',
landmarks: [[2,0],[5,0]]
}
];
function arrayToString(array) {
return "[" + array.join(',') + "]";
}
function showLandmarks() {
parks.forEach(function(park) {
var landmarks = park.landmarks;
if (!Array.isArray(landmarks[0])) {
landmarks = [landmarks];
}
var row = landmarks.map(function(lm) {
return arrayToString(lm);
})
var result = row.length > 1? "[" + row.join(",") + "]" : row.join(",");
console.log(park.name + ':', result);
})
}
showLandmarks();
Upvotes: 0
Reputation: 2957
remove JSON.stringify, if you don't need return a string instead of array of arrays.
const landmarks = (...points) => {
const result = [];
while (points.length) {
result.push(points.splice(0, 2));
}
return JSON.stringify(result);
};
// test
// Central Park:
console.log(landmarks(0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1));
// Juniper Valley Park:
console.log(landmarks(2, 0));
// Highland Park:
console.log(landmarks(2, 0, 5, 0));
Upvotes: 0
Reputation: 1
You don't need to use a loop here you can use a forEach on the Array and not have to worry about the i
index. This assumes for course that you always want the full array.
I suspect you actually want to have the []
printed around the coordinates.
if so you can use map
to add them to each coordinate set.
I would say that even though Juniper Valley Park only has one entry, that entry should still be within an Array for its own to keep data consistent.
{
name: "Juniper Valley Park",
landmarks: [[2, 0]]
},
This means we won't need any special logic to handle cases with only one coordinate.
var parks = [
{
name: 'Central Park',
landmarks: [[0,1],[1,1],[2,1],[3,1],[4,1],[5,1],[6,1],[7,1]]
},
{
name: 'Juniper Valley Park',
landmarks: [[2,0]]
},
{
name: 'Highland Park',
landmarks: [[2,0],[5,0]]
},]
function showLandmarks() {
parks.forEach(park =>
console.log(park.name +': ['+ park.landmarks.map(landmark => '[' + landmark +']') + ']'));
}
showLandmarks();
Output: Central Park: [0,1],[1,1],[2,1],[3,1],[4,1],[5,1],[6,1],[7,1]]
Juniper Valley Park: [[2,0]]
Highland Park: [[2,0],[5,0]]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Upvotes: 0
Reputation: 37775
When you use +
between two objects it internally calls toString and changes than concate the converted string
console.log([1,2]+[3,4])
You can use Object.fromEntries
var parks = [{ name: 'Central Park',landmarks: [[0,1],[1,1],[2,1],[3,1],[4,1],[5,1],[6,1],[7,1]]},{ name: 'Juniper Valley Park',landmarks: [2,0]},{ name: 'Highland Park',landmarks: [[2,0],[5,0]]},]
let obj = Object.fromEntries(parks.map(({
name,
landmarks
}) => [name, landmarks]))
console.log(obj)
Upvotes: 0
Reputation: 929
Instead of incrementing i
by one every loop, create a pair using parks[i]
and parks[i+1]
and add that pair to an accumulator array, then increment i
by two.
You will just have to perform a check that parks[i+1]
exists.
Upvotes: 0
Reputation: 17664
instead of manipulating the results, You could reduce
the original array to an object :
var parks = [
{
name: "Central Park",
landmarks: [[0, 1], [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [7, 1]]
},
{
name: "Juniper Valley Park",
landmarks: [2, 0]
},
{
name: "Highland Park",
landmarks: [[2, 0], [5, 0]]
}
];
const result = parks.reduce((acc, curr) => {
acc[curr.name] = curr.landmarks;
return acc;
}, {});
console.log(result);
Upvotes: 0