Utilize Angular.js or JavaScript to swap out one string with another

I have a JSON dataset and I want to update a specific value using Angular.js or JavaScript. Here is my code snippet:

$http({
    method:'POST',
    url:"php/getFilterCodeData.php",
    data:filterData,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(function successCallback(response){
    console.log('filter',response.data);
    $scope.last=[];
    for(var i=0;i<response.data.length;i++){
       $scope.arrCode=response.data[i].generated_code.split(',');
    }
        //console.log('arr',$scope.arrCode);
    for(var i=0;i<$scope.arrCode.length;i++){
        $scope.last.push($scope.arrCode[i].split("_").pop());
    }
    //console.log('last',$scope.last);
    var newStr=$scope.last[0]+"-"+$scope.last[$scope.last.length-1];

    //console.log('new str',newStr);
},function errorCallback(response) {
})

After logging the following data, this is what appears on the console:

filter [{
customer_name: "Big-Bazar"
expired_date: "23-12-2015"
generated_code: "medilink_global_01,medilink_global_02,..."
no_of_voucher: "40"
status: "generated"
voucher_amount: "3000"
voucher_code_id: "13"}]

To achieve the desired result, I need to replace the generated_code: value with newStr. The expected outcome should be:

generated_code:01-40

Answer №1

Within the realm of Javascript:

$scope.filter = filter;
$scope.filter[0].generate_code = newStr;

If you desire to display the string, simply incorporate {{ filter[0].generate_code }} into your template.

AngularJS diligently monitors modifications to variables in the scope and will automatically update them in the template when changes occur, making it quite seamless.

Answer №2

Here is the correct way to accomplish it:

dataResponse[i].updated_code=newValue;

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

Confirming the manipulation of Node.js callbacks

I have been working on a scheduling program in Node.js to retrieve JSON data about courses. As I'm relatively new to Node.js, I'm looking for ways to improve my code and avoid callback hell. I have already implemented the getJSON method. /*getJS ...

Calculate the total of a checkbox and hidden field combination using JavaScript

Checkboxes are often paired with hidden fields to ensure that the HTTP post includes a value even if the checkbox is left unchecked. For instance, when implementing a terms checkbox, both the checkbox and a hidden element can be used: <input type="c ...

The ::before pseudo element is malfunctioning when used in the makeStyles function

I am currently utilizing the makeStyles function in React, but I seem to be facing an issue where the content within the ::before pseudo-element is not displaying. Strangely enough, when the content is an image it works fine, but text does not render. Jus ...

How to include a file within another file in Node.js

When including one JavaScript file into another, I typically use the following syntax: var userControllerObj = require("../controller/userController"), userController = new userControllerObj.UserGatewayController(); I'm curious if I can u ...

Obtain the value of "Placeholder" using JavaScript in Internet Explorer without the need for JQuery

I have some custom Javascript code that checks for browser support of placeholders and creates them if not supported. This solution works on some older browsers, but not all, especially IE. The issue I am facing is retrieving the "Placeholder" value; curr ...

Is there a way to use Javascript to detect if the value of a control on an ASP.Net page has been modified?

Within my web page, I have integrated a FormView that transitions to Edit mode when the user clicks the edit button. To enhance user experience, I have implemented a JavaScript onbeforeunload function that triggers a confirmation dialog if the user tries t ...

Is there a way for me to retrieve form information?

I have encountered a challenge with my React app. The login form data appears to be empty when I attempt to send it to the backend. // Login component class submitLoginForm = (event) => { event.preventDefault(); const target = event.target; ...

Tips for implementing next-iron-session with personalized api route middleware in NextJS?

Currently, I am working on implementing session storage with next-iron-session and using Firebase for authentication. Every API route requires both the auth middleware and next-iron-session. This is my first time using NextJS and I have tried several appro ...

Tips on how to prevent certain classes from being impacted by a hue-rotate filter applied to all elements on a webpage

I am currently in the process of adding a feature that allows users to choose between a dark or light theme, as well as select a specific theme color for the app. The implementation involves using CSS filters such as invert(1) for the dark theme and hue-ro ...

Leveraging Cheerio in Node.js to locate a precise value within an option tag

I'm facing difficulties in selecting the exact number (in this case 7) which is the value of the option. This is what I'm attempting: var $ = cheerio.load(html); console.log($('ProductSelect').val($("option:contains('7')").v ...

`questioning the unusual behavior of document.onload(), seeking answers`

Before delving into this, let's avoid downvoting as this is not another typical question about window.onload vs document.onload. window.onload triggers once all DOM nodes are fully loaded, while document.onload triggers when all DOM nodes are ready w ...

Combining data types in TypeScript (incorporating new keys into an existing keyof type)

If I have a typescript type with keys: const anObject = {value1: '1', value2: '2', value3: '3'} type objectKeys = keyof typeof anObject and I want to add additional keys to the type without manually defining them, how can I ...

The suggestions for auto-complete in jQuery are not showing up as expected

I am looking to implement a feature where suggestions from the database are automatically populated in a text box as I type. Additionally, when I select a suggestion, other related text fields should be filled automatically. Below is the code snippet: Vi ...

The sticky header is malfunctioning due to a null offsetTop value

import React , {useRef, useEffect} from 'react' import './header.css' const nav_links =[ { path:'#home', display:'Home' }, { path:'#about', display:'About& ...

Difficulty in constructing an array from several Firebase Storage URLs

I'm attempting to retrieve multiple image URLs and store them in an array using Firebase Storage. However, I am facing issues accessing specific index positions within the testArray: var testArray = [] listAll(ref).then((res) => { res.item ...

The Ionic AngularJS http service is failing to update the controller

I'm struggling to understand why my controller is receiving an empty array after calling the service, which triggers an http call. Here's how I have set up my app: Here is my app.js file: //App.js .state('myApp.sermonlists', { u ...

Angular 2RC4 configurations for property setters (or change detection)

I am trying to trigger an action in component B when an action occurs in component A. Component A emits an event which serves as input for component B. While this setup works, it doesn't work consistently. Please take a look at the http://plnkr.co/e ...

Navigating spaces, tabs, and line breaks during ReactJS rendering

I have been attempting to display a string in a ReactJS Dialog box, which contains spaces and newlines represented by /n /t characters. My goal is to show the text exactly as it is with all the spaces and line breaks preserved. Despite trying various metho ...

How can we make sure a checkbox is only checked when there is corresponding data inputted?

I am facing an issue with my ng-repeater, where each row contains a checkbox and 3 text fields. I want to ensure that the user can only select the checkbox if they have entered data in all 3 text fields. If the user tries to select the checkbox without ent ...

Filtering with multiple attributes in Angular using GroupBy

I have a set of JSON data structured like this: [ { "id": 1, "address": "MG Road", "country": INDIA, "state": AP, "city": VIJ }, { "id": 2, "address": "Miyapur", "country": INDIA, ...