Использование помощника по неизменяемости с Array, Object, Map

Удалить теги HTML из строки. Где-то нам нужно разобрать некоторую строку, полученную некоторыми ответами типа Httpresponse с сервера.

Поэтому нам нужно проанализировать ее.

Здесь я покажу, как удалить html-теги из строка.

    // sample text with tags

    string str = "<html><head>sdfkashf sdf</head><body>sdfasdf</body></html>";



    // regex which match tags

    System.Text.RegularExpressions.Regex rx = new System.Text.RegularExpressions.Regex("<[^>]*>");



    // replace all matches with empty strin

    str = rx.Replace(str, "");



    //now str contains string without html tags
1
задан Finlay Beaton 31 March 2019 в 01:00
поделиться

1 ответ

Вы можете найти ответ на каждую отдельную операцию в отдельных вопросах, но не все изложены вместе.

1. Массив простых значений

import update from 'immutability-helper';

const oldArray = [1, 2, 3];

// add an item
const newArray = update(oldArray, {$push: [6, 7]}); 
// => [1, 2, 3, 6, 7]

// modify an item
const itemIndex = 1; // modify `2` value at index `1`
const newValue = 8;
const newArray = update(oldArray, { [itemIndex]: {$set: newValue} }); 
// => [1, 8, 3]

// remove an item
const itemIndex = 2; // delete `3` value at index `2`
const newArray = update(oldArray, {$splice: [[itemIndex, 1]] }); 
// => [1, 2]

2. Массив простых объектов

import update from 'immutability-helper';

const oldArray = [
    {name: 'Stacey', age: 55},
    {name: 'John', age: 77},
    {name: 'Kim', age: 62},
];

// add an item
const newArray = update(oldArray, {$push: [
    {name: 'Trevor', age: 45},
]});

// replace an item
const itemIndex = 1; // replace *John* at index `1`
const newValue = {name: 'Kevin', age: 25};
const newArray = update(oldArray, { [itemIndex]: {$set: newValue} });

// modify an item
const itemIndex = 1; // modify *John* at index `1`
const newArray = update(oldArray, {
    [itemIndex]: {$merge, {
        age: 85, // change John's age to 85
    }}
});         

// remove an item
const itemIndex = 0; // delete *Stacey* at index `0`
const newArray = update(oldArray, {$splice: [[itemIndex, 1]] } });

Вы можете использовать встроенную функцию findIndex () , чтобы найти индекс элемента на основе его свойств.

3. Массив находится в другом объекте

import update from 'immutability-helper';

const oldData = {
    city: {       
        people: [
            {name: 'Stacey', age: 55},
            {name: 'John', age: 77},
            {name: 'Kim', age: 62},
        ]
    }
};

// add an item
const newArray = update(oldArray, {
    city: {
        people: {$push: [
            {name: 'Trevor', age: 45},
        ]}
    }
});

// replace an item
const itemIndex = 1; // replace *John* at index `1`
const newValue = {name: 'Kevin', age: 25};
const newArray = update(oldArray, {
    city: {
        people: { 
            [itemIndex]: {$set: newValue} }}
        }
    }
);

// modify an item
const itemIndex = 1; // modify *John* at index `1`
const newArray = update(oldArray, { 
    city: {
        people: {
            [itemIndex]: {$merge, {
                age: 85, // change John's age to 85
            }}
        }
    }
});         

// remove an item
const itemIndex = 0; // delete *Stacey* at index `0`
const newArray = update(oldArray, {
    city: {
        people: {$splice: [[itemIndex, 1]] } 
    }
});

4. Объект объектов (хэши, неупорядоченные)

import update from 'immutability-helper';

const oldData = {
    'hash-stacey': {name: 'Stacey', age: 55},
    'hash-john': {name: 'John', age: 77},
    'hash-kim': {name: 'Kim', age: 62},
};

// add an item
const newData = update(oldData, {
    ['hash-trevor']: {$set: {name: 'Trevor', age: 45} }
});

// replace an item at a specific hash
const itemHash = 'hash-john';
const newValue = {name: 'Kevin', age: 25};
const newData = update(oldData, { [itemHash]: {$set: newValue} });

// modify an item
const itemHash = 'hash-john';
const newData = update(oldData, {
    [itemHash]: {$merge: {
        age: 85, // change John's age to 85
    }}
});         

// remove an item
const itemHash = 'hash-stacey';
const newData = update(oldData, {$unset: [itemHash] });

Карта объектов (хэши, упорядоченные)

import update from 'immutability-helper';

const oldData = new Map([
    ['hash-stacey', {name: 'Stacey', age: 55}],
    ['hash-john', {name: 'John', age: 77}],
    ['hash-kim', {name: 'Kim', age: 62}],
]);

// add an item
const newData = update(oldData, {$add: [
    ['hash-trevor', {name: 'Trevor', age: 45}],
]});

// replace an item at a specific hash
const itemHash = 'hash-john';
const newValue = {name: 'Kevin', age: 25};
const newData = update(oldData, { [itemHash]: {$set: newValue} });

// modify an item
const itemHash = 'hash-john';
/* please edit with better single update $merge or $apply */
const newValue = update(oldData.get(itemHash), {$merge: {
    age: 85, // change John's age to 85
}});
/* typescript needs to do `oldData as any` cast here */
const newData = update(oldData, { [itemHash]: {$set: newValue} });

// remove an item
const itemHash = 'hash-stacey';
const newData = update(oldData, {$remove: [itemHash] });
0
ответ дан Finlay Beaton 31 March 2019 в 01:00
поделиться
Другие вопросы по тегам:

Похожие вопросы: