How can I remove a specific item from obj?

Is there a way to create a function called removeStar that removes the star key from an object and outputs it in the format shown by the expectedOutput variable?

let input = {
  "p": {
    "pk1": "pv1",
    "pk2": "pv2",
    "c": {
      "*": {
        "ck1": "cv1",
        "ck2": "cv2"
      }
    }
  }
}

function removeStar(input) {
  // Looking for suggestions on how to implement this function.
}

let expectedOutput = {
  "p": {
    "pk1": "pv1",
    "pk2": "pv2",
    "c": {
      "ck1": "cv1",
      "ck2": "cv2"
    }
  }
};

console.log(removeStar(input));

Answer №1

Hopefully my assistance was beneficial

let obj = {
            "p": {
                "pk1": "pv1",
                "pk2": "pv2",
                "c": {
                    "*": {
                        "ck1": "cv1",
                        "ck2": "cv2"
                    }
                }
            }
        }

        function removeWildcard(obj) {
            for (const prop in obj.p) {
                if (typeof obj.p[prop] === 'object') {
                    for (const key in obj.p[prop]) {
                        if (key === '*') {
                            obj.p[prop] = obj.p[prop][key];
                        }
                    }
                }
            }
            console.log(obj)
        }

        removeWildcard(obj)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I encountered an issue while trying to integrate VideoJS with React

While using VideoJS, the video plays without any issues. However, I am facing an issue where the available source options are not being displayed as they should be. I attempted to use a plugin for the sources, but even then, the options didn't show u ...

What is the process for integrating a custom script prior to building in a react application?

I am facing an issue with the Chart library that I am using and in order to resolve it, I need to execute a specific script. The script can be found at this link: https://github.com/plouc/nivo/blob/master/scripts/patch-react-spring.js. I have considered ad ...

Compare object key and array in Javascript to create a new object

Can you aid me in achieving the following output by comparing var1 and var2, and obtaining the output based on var2 where the keys are provided in a string array? var1 = {a:1, b:2, c:3, d:4}; var2 = ['a', 'd']; The expected output is ...

I possess a pair of arrays, and I aim to transfer certain elements from Array1 to Array2 while maintaining their references

Looking to implement two JavaScript arrays using AngularJS. My goal is to transfer elements from Ar1 to Ar2, and then have any changes made to the values in Ar2 automatically update the values in Ar1 as well. ...

What is the process for using the fetch method in React to upload a file?

Currently, I am developing a react component that involves uploading an Excel file to a server. Although my approach seems correct, it returns an empty object when I check the request body in console. <input type="file" id="avatar" name="avatar" onChan ...

Creating a cutout effect on 3D text within a Geometry using Three.js

I am looking to convert my 3D text into Geometry so that I can utilize it with CSG (I have also used a Three.js CSG wrapper) in order to subtract it from another object, similar to the scenario described in this question. Here is my 3D text: loader.load( ...

What could be causing my Vuex state to remain unchanged even after the nuxtServerInit commit?

Although I've been utilizing the nuxtServerInit method to retrieve data from my Contentful CMS and commit the mutation to update the categories state object, I keep encountering an issue where categories remain empty even after attempting to display t ...

How to Call or Reference a Function in Javascript and React

import React from 'react'; import ReactDOM from 'react-dom'; class Vehicle extends React.Component { constructor(props) { super(props); this.state = { make: "Toyota", model: "Corolla", color: "blue", y ...

How to iterate through an array of objects in Javascript and extract an array of strings

I am dealing with an array of objects that looks like this: [{"key":"aaa","value":true},{"key":"bbb","value":false},{"key":"ccc","value":true}] My goal is to iterate through it and extract an array containing only the keys: ["aaa", "bbb", "ccc"] I am u ...

Error encountered with the Angular 2 routing system

Currently, I am facing an issue with my Angular 2 router module. Whenever I try to access the link /city, I encounter an error message saying 'ERROR Error: Uncaught (in promise): Error: Cannot activate an already activated outlet Error: Cannot activat ...

Expanding the Capability and Retrieving Data from the Parent Class Using JQuery

How can I access the super object while extending objects with $.extend? I need to extend an object, replace a method, and then invoke the overridden superclass method within the subclass method. ...

How to resolve undefined callback when passing it to a custom hook in React Native

I'm facing an issue with passing a callback to my custom hook: export default function useScreenshotDetection(onScreenshot) { useEffect(() => { ... onScreenshot(); ... }, []); } Strangely, the callback is not being detected ...

Disabling Express BodyParser for handling file uploads in Node.js

When developing my web application with Node.js + Express, I've found the connect BodyParser to be incredibly useful. However, I'm looking for a way to have more control over multipart form-data POSTS so that I can stream the input directly to an ...

The selected option in Bootstrap is displayed twice in the Bootstrap modal

I am facing an issue with Bootstrap Select-box showing multiple times in a bootstrap modal wizard. I have tried various solutions from Stack Overflow but none of them seem to work. A screenshot of the problem can be seen below: https://i.sstatic.net/glp3E ...

Is there a way to reverse the upside-down text generated by OffscreenCanvas.getContext().fillText()?

When using OffscreenCanvas.getContext().fillText() to generate text and then OffscreenCanvas.transferToImageBitmap() to create a map, I noticed that the text appeared flipped upside down when applied as a texture in a threejs project. The image clearly sho ...

Issue encountered while deploying Next.js application on vercel using the replaceAll function

Encountering an error during deployment of a next.js app to Vercel, although local builds are functioning normally. The issue seems to be related to the [replaceAll][1] function The error message received is as follows: Error occurred prerendering page &q ...

transform a JSON object into a new format

I am working with a JSON object that looks like this: [ { "AF28110": 33456.75, "AF27989": 13297.26 } ] My goal is to convert it into the following format: [ { "name": "AF28110", "price": 33456.75}, { "name": "AF27989", "price": 13297.26} ...

Ember.js: My fixtures array is getting cleared out based on the way it is being accessed

I have 2 different controllers in my application. The first one, KeywordsIndexController, is defined as follows: App.KeywordsIndexController = Em.ArrayController.extend({ contentBinding: "App.Keyword.FIXTURES" }); The second controller, KeywordsNew ...

Activate the class using Vue with the v-for directive

I'm curious about the v-for functionality. Why is it necessary to use this.activeClass = {...this.activeClass} to update the component? I noticed that the component does not update after this line of code. if (this.activeClass[index]) { ...

Encountering NPM install gyp errors in VSCode indicating that gyp is searching for Visual Studio

Running npm install on a local project has been quite challenging for me, as I keep encountering errors every time I try. Fortunately, some valuable information I found related to gyp and Python helped me make some progress. However, I'm currently fac ...