Home > Mobile >  How to replace array key word in an array
How to replace array key word in an array

Time:10-08

How can I replace className with class in this JSON array

    [{"type":"file","required":false,"label":"File Upload","className":"form-control","name":"file-1632987355592-0","access":false,"subtype":"file","multiple":false}]

I have tried str_replace(array_keys("className"), "class", $value); but it doesn't replace

The array is fetched from database

CodePudding user response:

This should do it

const arrayOfObjects = [
  {
    type: "file",
    required: false,
    label: "File Upload",
    className: "form-control",
    name: "file-1632987355592-0",
    access: false,
    subtype: "file",
    multiple: false
  }
];

const result = arrayOfObjects.map((object) => {
  object.class = object.className;
  delete object.className;
  return object;
});

CodePudding user response:

Use map to iterate over the array of objects. Destructure className from the rest of the object properties, and return an updated object.

const data = [{"type":"file","required":false,"label":"File Upload","className":"form-control","name":"file-1632987355592-0","access":false,"subtype":"file","multiple":false}];

const out = data.map(({ className, ...rest }) => {
  return { class: className, ...rest };
});

console.log(out);

CodePudding user response:

first: we will make new property called class and we well assign the className value to class property; second we will delete the old property; this is the code

// this is the object;
let obj = [
    {
        type: "file",
        required: false,
        label: "File Upload",
        className: "form-control",
        name: "file-1632987355592-0",
        access: false,
        subtype: "file",
        multiple: false,
    },
];
// first;
obj[0].class = obj[0].className;
// second
delete obj[0].className;

CodePudding user response:

That's because replace is only applicable to strings, you could essentially delete the property and re-assign a new one with class as the key;

Assuming your array has only one element the solution would be like this

const arr = [{"type":"file","required":false,"label":"File Upload","className":"form-control","name":"file-1632987355592-0","access":false,"subtype":"file","multiple":false}];

const obj = arr[0];
const val = obj.className; // reference to the actual value
delete obj['className'];
obj['class'] = val;

if you had multiple elements, you could just loop through the array and apply the same principle

const arr = [] // fill it

arr.map(x => {
 x.class = x.className // reference to original value;
 delete x.className;
 return x;
});

CodePudding user response:

I'd do this way!

var v = arr.map((item) => {
    let outputData = {...item};
    outputData['class'] = item.className;
    delete outputData.className;
    return outputData;
});

CodePudding user response:

If you want to change the JSON then it works without array_keys:

$json = str_replace("className", "class", $json);

However you should probably be more explicit:

$json = str_replace('"className":', '"class":', $json);

But to do it with an array just create the new one and unset the old one:

$array = json_decode($json, true);
$array[0]['class'] = $array[0]['className'];
unset($array[0]['className']);

If there are multiple occurrences then you would need to loop the array. The str_replace on the JSON though will replace them all.

  • Related