0

I am retrieving data from mysql and want to save each row of data in localStorage as array. The following is my nested array received at javascript end.

arr = [{image_id: "80", imagename: "Image1",firstx: "267", firsty: "403"}, 
      {image_id: "80", imagename: "Image1",firstx: "320", firsty: "470"}, 
      {image_id: "80", imagename: "Image2",firstx: "126", firsty: "237"}
       ]

From this I want to remove the image_id, imagename, firstx and firsty and return a result that is an array and contains only the values of each array. The desired output is

newarr =[[80,Image1,267,403],[80,Image1,320,470],[80,Image2,126,237]]

I have done the following:

var newarr = [];
for (var i = 0, l = arr.length; i < l; i++) {
    var keys = Object.keys(arr[i]);
    for (var j = 0, k = keys.length; j < k; j++) {
        newarr.push(arr[i][keys[j]]);
        }
}

console.log(newarr)

This returns each element of as an array. The resultant array will be pushed to localStorage as a nested array.

0

2 Answers 2

5

I'm guessing you want an array and not an object - map by Object.values:

const arr = [{image_id: "80", imagename: "Image1",firstx: "267", firsty: "403"}, 
  {image_id: "80", imagename: "Image1",firstx: "320", firsty: "470"}, 
  {image_id: "80", imagename: "Image2",firstx: "126", firsty: "237"}
];
console.log(arr.map(Object.values));

Sign up to request clarification or add additional context in comments.

Comments

0
var newarr = [];
for (var i = 0, l = arr.length; i < l; i++) {
    var keys = Object.keys(arr[i]);
    newarr[i] = [];
    for (var j = 0, k = keys.length; j < k; j++) {
        newarr[i].push(arr[i][keys[j]]);
        }
}

1 Comment

It can be done by just adding newarr[i] = []; in your code and pushing every j in newarr[i].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.