Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Accessing JSON keys#5810

Unanswered
GevinHasmitha asked this question inQ&A
Discussion options

I want to access and edit a nested key in the json, not the value. I tried unsetting the key value pair and setting a new pair with the key changed but this will add the new pair to the end of the json, i want to preserver the order of the json.

Is this supported?

You must be logged in to vote

Replies: 1 comment

Comment options

Yes, Lodash provides methods to manipulate nested keys in an object while preserving the order. One approach is to use the _.unset() function to remove the nested key, and then use the _.set() function to set a new value with the desired key in the same position.

Here's an example:

const _ = require('lodash');// Sample JSON objectconst json = {  level1: {    level2: {      key1: 'value1',      key2: 'value2'    }  }};// Remove the nested key_.unset(json, 'level1.level2.key1');// Set a new value with the desired key at the same position_.set(json, 'level1.level2.newKey', 'newValue');console.log(json);

which will output

{  level1: {    level2: {      key2: 'value2',      newKey: 'newValue'    }  }}
You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@GevinHasmitha@juanlet

[8]ページ先頭

©2009-2025 Movatter.jp